Java Code Examples for com.google.android.gms.maps.GoogleMap#addPolyline()

The following examples show how to use com.google.android.gms.maps.GoogleMap#addPolyline() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FreightTrackGoogleMapFragment.java    From ESeal with Apache License 2.0 6 votes vote down vote up
@Override
public void onMapReady(GoogleMap googleMap) {
    // Instantiates a new Polyline object and adds points to define a rectangle
    com.google.android.gms.maps.model.PolylineOptions rectOptions = new com.google.android.gms.maps.model.PolylineOptions()
            .add(new com.google.android.gms.maps.model.LatLng(-18.5186650000, 141.9748780000))
            .add(new com.google.android.gms.maps.model.LatLng(-18.5186650000, 144.9748780000))  // North of the previous point, but at the same longitude
            .add(new com.google.android.gms.maps.model.LatLng(-20.5186650000, 144.9748780000))  // Same latitude, and 30km to the west
            .add(new com.google.android.gms.maps.model.LatLng(-20.5186650000, 141.9748780000))  // Same longitude, and 16km to the south
            .add(new com.google.android.gms.maps.model.LatLng(-24.5186650000, 141.9748780000)); // Closes the polyline.

    rectOptions.width(8)
            .color(ContextCompat.getColor(getActivity(), R.color.red_500));

    // Get back the mutable Polyline
    com.google.android.gms.maps.model.Polyline polyline = googleMap.addPolyline(rectOptions);

    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new com.google.android.gms.maps.model.LatLng(-18.5186650000, 141.9748780000), 6));
}
 
Example 2
Source File: TrackPathUtils.java    From mytracks with Apache License 2.0 6 votes vote down vote up
/**
 * Add a path.
 * 
 * @param googleMap the google map
 * @param paths the existing paths
 * @param points the path points
 * @param color the path color
 * @param append true to append to the last path
 */
public static void addPath(GoogleMap googleMap, ArrayList<Polyline> paths,
    ArrayList<LatLng> points, int color, boolean append) {
  if (points.size() == 0) {
    return;
  }
  if (append && paths.size() != 0) {
    Polyline lastPolyline = paths.get(paths.size() - 1);
    ArrayList<LatLng> pathPoints = new ArrayList<LatLng>();
    pathPoints.addAll(lastPolyline.getPoints());
    pathPoints.addAll(points);
    lastPolyline.setPoints(pathPoints);
  } else {
    PolylineOptions polylineOptions = new PolylineOptions().addAll(points).width(5).color(color);
    Polyline polyline = googleMap.addPolyline(polylineOptions);
    paths.add(polyline);
  }
  points.clear();
}
 
Example 3
Source File: MainActivity.java    From ExamplesAndroid with Apache License 2.0 6 votes vote down vote up
@Override
public void onMapReady(GoogleMap googleMap) {

    //Creamos una localizacion
    LatLng lugar = new LatLng(19.3910038, -99.2837004);//creamos el punto de partida(DF)


   //Movemos la camara,El metodo newLatLngZoom esta sobrecargado
    //Este es uno de los metodos,y recibe una localizacion y el respectivo zoom como segundo parametro
    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(lugar, 20));


    // Indicamos los puntos para crear la polilinea
    //Ente este caso la ruta es del df - Guerrero - Michoacan y llegando a Guadalajara
    googleMap.addPolyline(new PolylineOptions().geodesic(true)
            .add(new LatLng(17.547789, -99.532435))  // Guerrero
            .add(new LatLng(19.1539267, -103.0220045))  // Michoacan
            .add(new LatLng(20.6998812, -103.405454))  // Guadalajara
            );
    //Utilidad para generar polilineas de google
    //https://developers.google.com/maps/documentation/utilities/polylineutility
}
 
Example 4
Source File: AirMapPolyline.java    From AirMapView with Apache License 2.0 5 votes vote down vote up
/**
 * Add this polyline to the given {@link GoogleMap} instance
 *
 * @param googleMap the {@link GoogleMap} instance to which the polyline will be added
 */
public void addToGoogleMap(GoogleMap googleMap) {
  // add the polyline and keep a reference so it can be removed
  googlePolyline = googleMap.addPolyline(new PolylineOptions()
      .addAll(points)
      .width(strokeWidth)
      .color(strokeColor));
}
 
Example 5
Source File: PolyActivity.java    From android-samples with Apache License 2.0 4 votes vote down vote up
/**
 * Manipulates the map when it's available.
 * The API invokes this callback when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera.
 * In this tutorial, we add polylines and polygons to represent routes and areas on the map.
 */
// [END EXCLUDE]
@Override
public void onMapReady(GoogleMap googleMap) {

    // Add polylines to the map.
    // Polylines are useful to show a route or some other connection between points.
    // [START maps_poly_activity_add_polyline_set_tag]
    // [START maps_poly_activity_add_polyline]
    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)));
    // [END maps_poly_activity_add_polyline]
    // [START_EXCLUDE silent]
    // Store a data object with the polyline, used here to indicate an arbitrary type.
    polyline1.setTag("A");
    // [END maps_poly_activity_add_polyline_set_tag]
    // Style the polyline.
    stylePolyline(polyline1);

    Polyline polyline2 = googleMap.addPolyline(new PolylineOptions()
            .clickable(true)
            .add(
                    new LatLng(-29.501, 119.700),
                    new LatLng(-27.456, 119.672),
                    new LatLng(-25.971, 124.187),
                    new LatLng(-28.081, 126.555),
                    new LatLng(-28.848, 124.229),
                    new LatLng(-28.215, 123.938)));
    polyline2.setTag("B");
    stylePolyline(polyline2);

    // [START maps_poly_activity_add_polygon]
    // Add polygons to indicate areas on the map.
    Polygon polygon1 = googleMap.addPolygon(new PolygonOptions()
            .clickable(true)
            .add(
                    new LatLng(-27.457, 153.040),
                    new LatLng(-33.852, 151.211),
                    new LatLng(-37.813, 144.962),
                    new LatLng(-34.928, 138.599)));
    // Store a data object with the polygon, used here to indicate an arbitrary type.
    polygon1.setTag("alpha");
    // [END maps_poly_activity_add_polygon]
    // Style the polygon.
    stylePolygon(polygon1);

    Polygon polygon2 = googleMap.addPolygon(new PolygonOptions()
            .clickable(true)
            .add(
                    new LatLng(-31.673, 128.892),
                    new LatLng(-31.952, 115.857),
                    new LatLng(-17.785, 122.258),
                    new LatLng(-12.4258, 130.7932)));
    polygon2.setTag("beta");
    stylePolygon(polygon2);
    // [END_EXCLUDE]

    // Position the map's camera near Alice Springs in the center of Australia,
    // and set the zoom factor so most of Australia shows on the screen.
    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-23.684, 133.903), 4));

    // Set listeners for click events.
    googleMap.setOnPolylineClickListener(this);
    googleMap.setOnPolygonClickListener(this);
}
 
Example 6
Source File: PolySimplifyDemoActivity.java    From android-maps-utils with Apache License 2.0 4 votes vote down vote up
@Override
protected void startDemo(boolean isRestore) {
    GoogleMap map = getMap();

    // Original line
    List<LatLng> line = PolyUtil.decode(LINE);
    map.addPolyline(new PolylineOptions()
            .addAll(line)
            .color(Color.BLACK));

    if (!isRestore) {
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(28.05870, -82.4090), 15));
    }

    List<LatLng> simplifiedLine;

    /*
     * Simplified lines - increasing the tolerance will result in fewer points in the simplified
     * line
     */
    double tolerance = 5; // meters
    simplifiedLine = PolyUtil.simplify(line, tolerance);
    map.addPolyline(new PolylineOptions()
            .addAll(simplifiedLine)
            .color(Color.RED - ALPHA_ADJUSTMENT));

    tolerance = 20; // meters
    simplifiedLine = PolyUtil.simplify(line, tolerance);
    map.addPolyline(new PolylineOptions()
            .addAll(simplifiedLine)
            .color(Color.GREEN - ALPHA_ADJUSTMENT));

    tolerance = 50; // meters
    simplifiedLine = PolyUtil.simplify(line, tolerance);
    map.addPolyline(new PolylineOptions()
            .addAll(simplifiedLine)
            .color(Color.MAGENTA - ALPHA_ADJUSTMENT));

    tolerance = 500; // meters
    simplifiedLine = PolyUtil.simplify(line, tolerance);
    map.addPolyline(new PolylineOptions()
            .addAll(simplifiedLine)
            .color(Color.YELLOW - ALPHA_ADJUSTMENT));

    tolerance = 1000; // meters
    simplifiedLine = PolyUtil.simplify(line, tolerance);
    map.addPolyline(new PolylineOptions()
            .addAll(simplifiedLine)
            .color(Color.BLUE - ALPHA_ADJUSTMENT));


    // Triangle polygon - the polygon should be closed
    ArrayList<LatLng> triangle = new ArrayList<>();
    triangle.add(new LatLng(28.06025,-82.41030));  // Should match last point
    triangle.add(new LatLng(28.06129,-82.40945));
    triangle.add(new LatLng(28.06206,-82.40917));
    triangle.add(new LatLng(28.06125,-82.40850));
    triangle.add(new LatLng(28.06035,-82.40834));
    triangle.add(new LatLng(28.06038, -82.40924));
    triangle.add(new LatLng(28.06025,-82.41030));  // Should match first point

    map.addPolygon(new PolygonOptions()
            .addAll(triangle)
            .fillColor(Color.BLUE - ALPHA_ADJUSTMENT)
            .strokeColor(Color.BLUE)
            .strokeWidth(5));

    // Simplified triangle polygon
    tolerance = 88; // meters
    List simplifiedTriangle = PolyUtil.simplify(triangle, tolerance);
    map.addPolygon(new PolygonOptions()
            .addAll(simplifiedTriangle)
            .fillColor(Color.YELLOW - ALPHA_ADJUSTMENT)
            .strokeColor(Color.YELLOW)
            .strokeWidth(5));

    // Oval polygon - the polygon should be closed
    List<LatLng> oval = PolyUtil.decode(OVAL_POLYGON);
    map.addPolygon(new PolygonOptions()
            .addAll(oval)
            .fillColor(Color.BLUE - ALPHA_ADJUSTMENT)
            .strokeColor(Color.BLUE)
            .strokeWidth(5));

    // Simplified oval polygon
    tolerance = 10; // meters
    List simplifiedOval= PolyUtil.simplify(oval, tolerance);
    map.addPolygon(new PolygonOptions()
            .addAll(simplifiedOval)
            .fillColor(Color.YELLOW - ALPHA_ADJUSTMENT)
            .strokeColor(Color.YELLOW)
            .strokeWidth(5));
}
 
Example 7
Source File: GoogleMapShapeConverter.java    From geopackage-android-map with MIT License 2 votes vote down vote up
/**
 * Add a Polyline to the map
 *
 * @param map      google map
 * @param polyline polyline options
 * @return polyline
 */
public static Polyline addPolylineToMap(GoogleMap map,
                                        PolylineOptions polyline) {
    return map.addPolyline(polyline);
}