본문

180905(수) - Google Maps (Polylines and Polygons to Represent Routes and Areas)

Google Maps


Polylines and Polygons to Represent Routes and Areas


Sample code

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   
package="com.example.polygons">

   
<application
       
android:allowBackup="true"
       
android:icon="@mipmap/ic_launcher"
       
android:label="@string/app_name"
       
android:supportsRtl="true"
       
android:theme="@style/AppTheme">

       
<meta-data
           
android:name="com.google.android.gms.version"
           
android:value="@integer/google_play_services_version" />

       
<!--
             The API key for Google Maps-based APIs.
        -->

       
<meta-data
           
android:name="com.google.android.geo.API_KEY"
           
android:value="@string/google_maps_key" />

       
<activity
           
android:name="com.example.polygons.PolyActivity"
           
android:label="@string/title_activity_maps">
           
<intent-filter>
               
<action android:name="android.intent.action.MAIN" />
               
<category android:name="android.intent.category.LAUNCHER" />
           
</intent-filter>
       
</activity>
   
</application>
</manifest>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:id="@+id/map"
   
android:name="com.google.android.gms.maps.SupportMapFragment"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
tools:context="com.example.polygons.PolyActivity" />


Add a polyline to draw a line on the map

1. Create PolylineOptions object and add points

2. add polyline to the map

GoogleMap.addPolyline()

Polyline polyline1 = googleMap.addPolyline(new PolylineOptions()
       
.clickable(true)
       
.add(
               
new LatLng(-35.016, 143.321),
               
new LatLng(-34.747, 145.592),
               
new LatLng(-34.364, 147.891),
               
new LatLng(-33.501, 150.217),
               
new LatLng(-32.306, 149.248),
               
new LatLng(-32.491, 147.309)));


Store arbitrary data with a polyline

Polyline.setTag()

Polyline.getTag()

Polyline polyline1 = googleMap.addPolyline(new PolylineOptions()
       
.clickable(true)
       
.add(
               
new LatLng(-35.016, 143.321),
               
new LatLng(-34.747, 145.592),
               
new LatLng(-34.364, 147.891),
               
new LatLng(-33.501, 150.217),
               
new LatLng(-32.306, 149.248),
               
new LatLng(-32.491, 147.309)));
// Store a data object with the polyline, used here to indicate an arbitrary type.
polyline1
.setTag("A");


Add custom styling to your polyline

- start, end cap을 line에 적용 가능

- 아래 코드는 start cap 에 custom arrow를 적용했다.

- width, color, jointType도 지정함

private static final int COLOR_BLACK_ARGB = 0xff000000;
private static final int POLYLINE_STROKE_WIDTH_PX = 12;

private void stylePolyline(Polyline polyline) {
   
String type = "";
   
// Get the data object stored with the polyline.
   
if (polyline.getTag() != null) {
        type
= polyline.getTag().toString();
   
}

   
switch (type) {
       
// If no type is given, allow the API to use the default.
       
case "A":
           
// Use a custom bitmap as the cap at the start of the line.
            polyline
.setStartCap(
                   
new CustomCap(
                           
BitmapDescriptorFactory.fromResource(R.drawable.ic_arrow), 10));
           
break;
       
case "B":
           
// Use a round cap at the start of the line.
            polyline
.setStartCap(new RoundCap());
           
break;
   
}

    polyline
.setEndCap(new RoundCap());
    polyline
.setWidth(POLYLINE_STROKE_WIDTH_PX);
    polyline
.setColor(COLOR_BLACK_ARGB);
    polyline
.setJointType(JointType.ROUND);
}


Handle click events on the polyline

1. Polyline.setClickable()

- default 는 not clickable


2. Implement OnPolylineClickListener interface & set listener GoogleMap.setOnPolylineClickListener() on the map

public class PolyActivity extends AppCompatActivity
       
implements
               
OnMapReadyCallback,
               
GoogleMap.OnPolylineClickListener,
               
GoogleMap.OnPolygonClickListener {

   
@Override
   
public void onMapReady(GoogleMap googleMap) {
       
// Add a polyline to the map.
       
Polyline polyline1 = googleMap.addPolyline((new PolylineOptions())
               
.clickable(true)
               
.add(new LatLng(-35.016, 143.321),
                       
new LatLng(-34.747, 145.592),
                       
new LatLng(-34.364, 147.891),
                       
new LatLng(-33.501, 150.217),
                       
new LatLng(-32.306, 149.248),
                       
new LatLng(-32.491, 147.309)));

       
// Set listeners for click events.
        googleMap
.setOnPolylineClickListener(this);
        googleMap
.setOnPolygonClickListener(this);
   
}
}

3. Override onPolylineClick()

private static final PatternItem DOT = new Dot();
private static final PatternItem GAP = new Gap(PATTERN_GAP_LENGTH_PX);
//
// Create a stroke pattern of a gap followed by a dot.
private static final List<PatternItem> PATTERN_POLYLINE_DOTTED = Arrays.asList(GAP, DOT);

@Override
public void onPolylineClick(Polyline polyline) {
   
// Flip from solid stroke to dotted stroke pattern.
   
if ((polyline.getPattern() == null) || (!polyline.getPattern().contains(DOT))) {
        polyline
.setPattern(PATTERN_POLYLINE_DOTTED);
   
} else {
       
// The default pattern is a solid stroke.
        polyline
.setPattern(null);
   
}

   
Toast.makeText(this, "Route type " + polyline.getTag().toString(),
           
Toast.LENGTH_SHORT).show();
}


공유

댓글