How to Draw Dashed Polyline with Android Google Map Sdk V2

Draw dotted polyline on Google Map - Android

As of February 2017, Google released a new set of customizations for polylines and polygons in Google Maps Android API v2. Now you can create dashed or dotted polylines.

See information about new features in the Shapes Guide. See an example in the Polylines and Polygons tutorial.

You can also read the corresponding blog post here:

https://maps-apis.googleblog.com/2017/02/styling-and-custom-data-for-polylines.html

How to draw dashed polyline with android google map sdk v2?

It is not possible in current release. Follow this issue for updates: https://code.google.com/p/gmaps-api-issues/issues/detail?id=4633

UPDATE

Recently, Google implemented this feature for polylines in Google Maps Android API v2 and marked issue 4633 as Fixed.

See information about stroke patterns in the Shapes Guide. See an example in the Polylines and Polygons tutorial.

You can also read the corresponding blog post here:

https://maps-apis.googleblog.com/2017/02/styling-and-custom-data-for-polylines.html

Android Google maps SDK draw a polyline with a dashed line

I'm afraid this answer wont suit you:

Currently there is no easy way of doing this on Google Maps v2. If you want to use the tools provided by Google you must split your Polyline in to several small ones, this SO question has a example: How to draw dashed polyline with android google map sdk v2?

If you really want dashed lines and need to draw many (really many) I would suggest creating a overlay and draw on that instead and move it with the map. However it isn't an easy approach considering e.g scaling and rotating of the map. But for small - medium amount of dashed lines it is not worth the pain.

There will likely be a dashed feature in Google Maps V3 considering that Google Maps JS v3 has it.

How to draw dotted polylines using Google Maps - Android

In February 2017 Google released a new set of customizations for polylines and polygons in Google Maps Android API v2. Particularly you can now create dashed and dotted polylines.

See information about new features in the Shapes Guide. See an example in the Polylines and Polygons tutorial.

You can also read the corresponding blog post here:

https://maps-apis.googleblog.com/2017/02/styling-and-custom-data-for-polylines.html

Can I draw a curved dashed line in Google Maps Android?

You can implement the curved dashed polyline between two points. For this purpose you can use Google Maps Android API Utility Library that has SphericalUtil class and apply some math in your code to create a polyline.

You have to include the utility library in your gradle as

compile 'com.google.maps.android:android-maps-utils:0.5'.

Please have a look at my sample Activity and function showCurvedPolyline (LatLng p1, LatLng p2, double k) that constructs dashed curved polyline between two points. The last parameter k defines curvature of the polyline, it can be >0 and <=1. In my example I used k=0.5

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
private LatLng sydney1;
private LatLng sydney2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

mMap.getUiSettings().setZoomControlsEnabled(true);

// Add a marker in Sydney and move the camera
sydney1 = new LatLng(-33.904438,151.249852);
sydney2 = new LatLng(-33.905823,151.252422);

mMap.addMarker(new MarkerOptions().position(sydney1)
.draggable(false).visible(true).title("Marker in Sydney 1"));
mMap.addMarker(new MarkerOptions().position(sydney2)
.draggable(false).visible(true).title("Marker in Sydney 2"));

mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney1, 16F));

this.showCurvedPolyline(sydney1,sydney2, 0.5);
}

private void showCurvedPolyline (LatLng p1, LatLng p2, double k) {
//Calculate distance and heading between two points
double d = SphericalUtil.computeDistanceBetween(p1,p2);
double h = SphericalUtil.computeHeading(p1, p2);

//Midpoint position
LatLng p = SphericalUtil.computeOffset(p1, d*0.5, h);

//Apply some mathematics to calculate position of the circle center
double x = (1-k*k)*d*0.5/(2*k);
double r = (1+k*k)*d*0.5/(2*k);

LatLng c = SphericalUtil.computeOffset(p, x, h + 90.0);

//Polyline options
PolylineOptions options = new PolylineOptions();
List<PatternItem> pattern = Arrays.<PatternItem>asList(new Dash(30), new Gap(20));

//Calculate heading between circle center and two points
double h1 = SphericalUtil.computeHeading(c, p1);
double h2 = SphericalUtil.computeHeading(c, p2);

//Calculate positions of points on circle border and add them to polyline options
int numpoints = 100;
double step = (h2 -h1) / numpoints;

for (int i=0; i < numpoints; i++) {
LatLng pi = SphericalUtil.computeOffset(c, r, h1 + i * step);
options.add(pi);
}

//Draw polyline
mMap.addPolyline(options.width(10).color(Color.MAGENTA).geodesic(false).pattern(pattern));
}

}

You can download a sample project with complete code from GitHub

https://github.com/xomena-so/so43305664

Just replace my API key with yours in the app/src/debug/res/values/google_maps_api.xml

Sample Image

How to draw dotted poly lines when inside the home or building in flutter

Try to below code for poly line with dashed

      GoogleMap(
initialCameraPosition: _kGooglePlex,
onMapCreated: _onMapCreated,
markers: markers,
polylines: {
Polyline(
polylineId: PolylineId('1'),
color: ThemeColors.primary,
width: 2,
patterns: [
PatternItem.dash(8),
PatternItem.gap(15),
],
points: const [
LatLng(37.42796133580664, -122.085749655962),
LatLng(36.42796133580664, -123.08575),
],
),
},
),

For more read this article



Related Topics



Leave a reply



Submit