Google map v2 marker not starting with my location - android

SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
googleMap = fm.getMap();
googleMap.setMyLocationEnabled(true);
Marker marker = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(location.getLatitude(),location.getLongitude()))
.title("Me")
.snippet("Population: 776733"));
When I run the code,google map is supposed to show the place where I am with the marker,but the map is opening up with some other place,no my current location,and I have to move to my location manually to find the marker.

You should animateCamera on that particular location Marker like
LatLng current_loc= new LatLng(lat, lng);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(current_loc, 18.0f));
For more information go to https://developers.google.com/maps/documentation/android/views#moving_the_camera

try this,
googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(your latitude, you longitude)));

Related

Look if current location is near marker

I have a Google Maps API Activity and want the markes only to be clickable if the user is in a certain radius to them.
How is the best way to do this?
This is how I set my markers:
Marker marker1 = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(49.793012, 9.926201))
.title(getString(R.string.Title1))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
Marker marker2 = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(49.792742, 9.939118))
.title(getString(R.string.Title2))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
Marker marker3 = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(49.793349, 9.932558))
.title(getString(R.string.Title3))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
Solution code:
LatLng markerPosition = marker.getPosition();
myLocation = locationManager.getLastKnownLocation(provider);
Location markerLoc = new Location("marker");
markerLoc.setLatitude(markerPosition.latitude);
markerLoc.setLongitude(markerPosition.longitude);
float meters = myLocation.distanceTo(markerLoc);
You can get the LatLng of the markers by marker.getPosition(); then you can compare it with the CurrentLocation. To do this, you can check this post, and next you can make clickable or notclickable a marker as follows:
getMap().setOnMarkerClickListener(new OnMarkerClickListener() {
public boolean onMarkerClick(Marker marker) {
onMapClick(marker.getPosition());
return true;
}
});
If you return true for the function, it means that you've accepted the occurred click event on your marker; otherwise, you've not accepted it.
Moreover, you can do it with:
MarkerOptions.clickable and Marker.setClickable.
This is just a guide who how I would do what you want. hope it helps. :)

Show route direction in Google Maps

googleMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
googleMap.addMarker(new MarkerOptions()
.position(
new LatLng(Float.parseFloat(latitude), Float
.parseFloat(longitude)))
.title("Marker").title(title));
**googleMap.addPolyline(new PolylineOptions()
.add(new LatLng( Double.parseDouble(latitude1), Double.parseDouble(longitude1)),
new LatLng( Double.parseDouble(latitude), Double.parseDouble(longitude)))
.width(5).color(Color.BLUE).geodesic(true));;**
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
cameraLatLng, 17));
So basically that's what I have done, but what that code does is it draws a line from a source to a destination. However, I do not want to draw a line, I just want to show route direction.
like this picture http://s7.postimg.org/dwdbkq6h7/Screenshot_2015_04_14_20_40_58.png
You can use the Google Direction API to find a route between two points.

Launch google maps after clicking Marked

I have got the following code, and what I want to do is to open "Google Maps" APP [How to go] to the marker I have set up:
// S E T M A P P O I N T
mLocationClient = new LocationClient(this, this, this);
mapFragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));
map = mapFragment.getMap();
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.getUiSettings().setZoomControlsEnabled(true);
map.getUiSettings().setCompassEnabled(true);
map.setOnMapClickListener(this);
map.setMyLocationEnabled(true);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), ZOOM));
map.clear();
addressText = GetGeolocation.getgeocoder(Date_Accept.this, latitude, longitude);
Marker marker = map.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.title(addressText)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
marker.showInfoWindow();
How can I open "Google Maps" app to get the route to that point?
you should be able to fire an intent with the maps url and let android handle it for you
read about that here

How to add multiple marker and overlay in v2 google map?

I am using google map v2 api. In that i need to show multiple markers and multiple overlays. I don't have any idea about this. If anybody knows answer kindly share your thoughts. Thank you.
For single marker and overlay i use this code
hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
.title("Hamburg")
.snippet("Kiel is cool")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
Use like this
LatLng one = new LatLng(2.40744, 77.014702);//Latitude and long points
LatLng two = new LatLng(2.407440, 77.014702);
LatLng three = new LatLng(2.4013, 76.951340000000002);
.......
Similarly u can use more lat and long
myMarkerOne = gm.addMarker(new MarkerOptions()
.position(one)//use LatLng obj
.title("C")
.snippet("dsfd")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
myMarkerTwo = gm.addMarker(new MarkerOptions()
.position(two)
.title("C")
.snippet("dsfds")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
myMarkerThree = gm.addMarker(new MarkerOptions()
.position(three)
.title("A")
.snippet("dfd")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
and so on..
Try this one.It will be better
String values[]={"2.40744, 77.014702","6.407440, 77.014702","10.4013, 76.951340000000002"};
GoogleMap googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment supportMapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
// Getting a reference to the map
googleMap = supportMapFragment.getMap();
for(int i=0;i<values.length;i++)
{
String s[] = values[i].split(",");
String v1 = s[0];
String v2 = s[1];
googleMap.addMarker(new MarkerOptions()
.position(
new LatLng(Double.valueOf(v1),Double.valueOf(v2)))
.title("Hi")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
}
}

how to zoom and add marker to android mapfragment

I am displaying a map at my android application (google maps api v2).
I want to manipulate the map in order to show a specific location, zoom and marker.
can anyone give an example of manipulation mapfragment
To get map instance in code do this:
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
or:
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
depending on whether you used SupportMapFragment or MapFragment in your XML file.
Then to add a marker to it:
Marker newmarker = map.addMarker(new MarkerOptions().position(latlng).title("marker title").icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_purpul)));
To zoom the map:
CameraPosition cameraPosition = new CameraPosition.Builder().target(latlng).zoom(14.0f).build();
CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
map.moveCamera(cameraUpdate);

Categories

Resources