Draw a line on a map but the line not a road - android

How can I draw a line on a map to detect a live location on that line.
but the line won't be on a road (street), it is a well known line on the map
using google map API.

You can draw a line with
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)));
Check documentation

Related

android: google map api always starts showing from africa

I am using a map fragment to show a map. But when the map starte it shows some location in africa and then shifts to the place i wanted. i have tried few solutions in the stackoverflow, which didnt work, it still starts from africa
CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(26.588527, 77.519531));
CameraUpdate zoom = CameraUpdateFactory.zoomTo(4);
mMap.moveCamera(center);
mMap.animateCamera(zoom);
another solution:
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(26.588527, 77.519531), 4));
You can configure initial map state:
using cameraTargetLat, cameraTargetLng, cameraZoom XML attributes, e.g.:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraTargetLat="26.588527"
map:cameraTargetLng="77.519531"
map:cameraZoom="4"/>
programmatically using GoogleMapOptions, e.g.:
CameraPosition indiaPosition = new CameraPosition.Builder()
.target(new LatLng(26.588527, 77.519531))
.zoom(4)
.build();
GoogleMapOptions mapOptions = new GoogleMapOptions().camera(camera)
SupportMapFragment map = SupportMapFragment.newInstance(mapOptions);
Try this:
//setting India region in google maps
LatLngBounds INDIA = new LatLngBounds(
new LatLng(7.2, 67.8), new LatLng(36.5, 93.8));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(INDIA.getCenter(), 5));
this would set India region on the map.
Try this :
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(26.588527, 77.519531), 11.5f), 4500, null);
works very well !!

Create MapView Programmatically in Android MapsV2

Android: While creating Map
I am getting the Blank page.I have a valid API key also and set it in Manifest.
my activity.
relativeLayout = new RelativeLayout(context);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
relativeLayout.setLayoutParams(layoutParams);
GoogleMapOptions googleMapOptions = new GoogleMapOptions();
googleMapOptions.mapType(GoogleMap.MAP_TYPE_NORMAL)
.compassEnabled(false).rotateGesturesEnabled(false)
.tiltGesturesEnabled(false);
googleMapOptions.camera(new CameraPosition(new LatLng(0, 0), 3, 0,
0));
mapView = new MapView(context, googleMapOptions);
mapView.onCreate(new Bundle());
relativeLayout.addView(mapView);
Double[][] latlang = mapData.getLatlang();
marker = new Marker[mapData.getLatlang().length];
for (int i = 0; i < mapData.getLatlang().length; i++) {
this.marker[i] = mapView.getMap()
.addMarker(
new MarkerOptions()
.position(
new LatLng(latlang[i][0],
latlang[i][1]))
.title(" ")
.snippet(" "));
}
I had the same issue, because I was creating the MapView inside a Fragment and forgot to add the onResume method to the fragment code.
mapView map = new MapView(getActivity(), options));
map.setClickable(true);
map.onCreate(savedInstanceState);
map.onResume();
Hope this helps someone
Add setContentView(relativeLayout); after this line relativeLayout.addView(mapView); It'll solve your problem.

how to use custom pointers in osmdroid?

I am using OSMDroid and this code gives default markers to point my location. How do i put custom markers in place of the default markers? How do i import a new drawable?
anotherOverlayItemArray = new ArrayList<OverlayItem>();
anotherOverlayItemArray.add(new OverlayItem("KTM2", "KTM2", myLocation));
ItemizedIconOverlay<OverlayItem> anotherItemizedIconOverlay = new
ItemizedIconOverlay<OverlayItem>( this, anotherOverlayItemArray,myOnItemGestureListener);
mapView.getOverlays().clear();
mapView.getOverlays().add(anotherItemizedIconOverlay);
mapView.invalidate();
You can set a specific marker to each OverlayItem:
OverlayItem item = new OverlayItem("KTM2", "KTM2", myLocation);
Drawable myMarker = getResources().getDrawable(markerResId);
item.setMarker(myMarker);
anotherOverlayItemArray.add(item);
You can also get rid of the ItemizedIconOverlay/OverlayItem approach by using the OSMBonusPack Marker.

Update color programmatically

I draw lines in the Google Map and how can I update the color in the program?
map.addPolyline(new PolylineOptions()
.add(latlngArray)
.width(5)
.color(Color.CYAN)
.geodesic(true));
Color.CYAN is the line color. Under certain condition, I need to show the line with different colors. How can I change the Color.CYAN with another color on runtime?
You can try doing something like this:
map.addPolyline(new PolylineOptions()
.add(latlngArray)
.width(5)
.color( Color.parseColor( "#AARRGGBB" ) )
.geodesic(true));
Where A is alpha, and RGB is your typical red, green, blue color mapping. Then you can use any string color you want.
mGoogleMap.addPolyline(new PolylineOptions()
.add(latlngArray)
.width(5)
.color( getResources().getColor(R.color.colorPrimary) )
.geodesic(true));
It's for working me

OSMdroid add custom icons to ItemizedOverlay

I am using ItemizedIconOverlay class and I'm currently displaying events on the map along with the user's position with the same default icon.
How do I change the icon set for each overlay?
Is there something similar to the google.maps example:
drawable = getResources().getDrawable(R.drawable.marker);
drawable3 = getResources().getDrawable(R.drawable.disruption);
drawable2 = getResources().getDrawable(R.drawable.marker_me);
itemizedOverlay = new MyItemizedOverlay(drawable, mapView);
itemizedOverlay2 = new MyItemizedOverlay(drawable2, mapView);
itemizedOverlay3 = new MyItemizedOverlay(drawable3, mapView);
I had each itemizedOverlay have its own marker...
How do I do this with Open Street Maps?
mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
this.mMyLocationOverlay = new ItemizedIconOverlay<OverlayItem>(mItems, new Glistener(), mResourceProxy);
Thank you for your help and its a shame not much support is available online for this open source project ...
Presumably your mItems is an ArrayList of OverlayItems created like:
mItems = new ArrayList<OverlayItem>();
To this list you will be adding individual OveralyItems, so when you create each item you can do it like this, setting the marker before you add it to the list:
OverlayItem olItem = new OverlayItem("Here", "SampleDescription", point);
Drawable newMarker = this.getResources().getDrawable(R.drawable.mymarker);
olItem.setMarker(newMarker);
mItems.add(olItem);
where mymarker is a .png in your drawables folder.
Update - to set default marker for whole overlay, change
this.mMyLocationOverlay = new ItemizedIconOverlay<OverlayItem>(mItems, new Glistener(), mResourceProxy);
to
this.mMyLocationOverlay = new ItemizedIconOverlay<OverlayItem>(mItems, newMarker, new Glistener(), mResourceProxy);
where newMarker is as before

Categories

Resources