How to show latitude and longitude which I received from server on android map. I implemented google map V2 but don't know how to show gps coordinates on map. I came here after a lot of research and wasn't able to find a single reasonable solution for it. Can anyone please help me out?
So far my code of latitude and longitude values which I am reviving from server.
String lat, lon;
double l1,l2;
DB db = new DB(getApplicationContext());
HashMap<String,String> gps = new HashMap<String, String>();
gps = db.getGps();
lat = user.get("lati");
lon = user.get("longi");
Okay when I try to add marker on map according to latitude and longitude from server my app got force close and gives NullPointerException.
free.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String lat, lon;
double l1,l2;
DB db = new DB(getApplicationContext());
HashMap<String,String> gps = new HashMap<String, String>();
gps = db.getGps();
lat = user.get("lati");
lon = user.get("longi");
l1 = Double.parseDouble(lat);
l2 = Double.parseDouble(lon);
map.addMarker(new MarkerOptions().position(new LatLng(l1, l2))
.title("My Map Title"));
Your "map" variable may not be initialized. I add a marker like this:
SupportMapFragment smf = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mMap = smf.getMap();
if ( mMap != null ) {
LatLng newLatLong = new LatLng(dblLat, dblLong);
mMap.addMarker(new MarkerOptions().position(newLatLong)
.title(name)
.snippet(fullStrAddr));
}
Add a marker like this
Marker mymarker= map.addMarker(new MarkerOptions().position(new LatLng(lat, lon))
.title("My Map Title"));
This will set a marker at the latitude and longitude which was sent from the server..
Try it..
Related
There is a Google map on my Android and I am new to the new Google Maps API. There is an initial marker on it.
This is the method to show the map and the initial marker:
#Override
public void onMapReady(GoogleMap map) {
//map is ready
// latitude and longitude
double latitude = latitud_del_hotel;
double longitude =longitud_del_hotel;
// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title(nombre_del_hotel).icon(BitmapDescriptorFactory.fromResource(R.drawable.poi));
// Changing marker icon
// marker.icon(BitmapDescriptorFactory
// .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
// adding marker
map.addMarker(marker);
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(latitud_del_hotel, longitud_del_hotel)).zoom(15).build();
map.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}
Now, I need to put other markers on it. These markers are JSON objects that I am retrieving on another method from the activity.
May be my question is stupid, but I don't know how to add markers on the same map but created on another method.
This is how am I creating a new marker on another method:
hotel.setNombre(obj.getString("nombre_hotel"));
hotel.setLatitud(obj.getDouble("latitud_hotel"));
hotel.setLongitud(obj.getDouble("longitud_hotel"));
hotel.setDireccion(obj.getString("direccion_hotel"));
String nombre = obj.optString("nombre_hotel");
Log.d("NOMBRE=", nombre);
String latitud = obj.optString("latitud_hotel");
double latitud_del_hotel = Double.parseDouble(latitud);
String longitud = obj.optString("longitud_hotel");
double longitud_del_hotel = Double.parseDouble(longitud);
// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitud_del_hotel, longitud_del_hotel)).title(nombre).icon(BitmapDescriptorFactory.fromResource(R.drawable.poi));
map.addMarker(marker);
But obviously, the line map.addMarker(marker); shows a warning: Cannot resolve symbol map.
Sorry if it is easy to solve, but I cannot.
Thank you.
In your onMapReady(...) add a field reference to the returned map.
public void onMapReady(GoogleMap map) {
this.googleMap = map;
Then, you can do:
// create marker
if(this.googleMap != null) {
MarkerOptions marker = new MarkerOptions(...);
this.googleMap.addMarker(marker);
}
This might create what's known as a race condition - if your network call to retrieve hotel location information returns a result before the map is ready, you will never get the results added into the map.
You could combat this by doing:
// Create this as a field:
List<MarkerOptions> markerOptions = new ArrayList<>();
MarkerOptions marker = new MarkerOptions(...);
if(this.googleMap != null) {
this.googleMap.addMarker(marker);
} else {
this.markerOptions.add(marker);
}
Then, at the bottom of onMapReady(...):
if(this.markerOptions != null && !this.markerOptions.isEmpty()) {
for(MarkerOption markerOption : this.markerOptions) {
this.googleMap.addMarker(markerOption);
}
}
I have a checkboxlist where the user can select some routes then a reponse is being getting from the server. I have method gotoLocation to upadte the location of the markers as well to add a new marker in the map when a new one is being inserted into the table on the serverside with the same route.
I had problem with adding a new marker to the map before so when I inserted data for a new marker with the same selected route in my database table, the new inserted one was not added to the map. I stored the id and Marker in the HashMap before I solved this problem by storing the data of the marker as String in the HashMap in this case the new inserted data is being added to the map as marker.
Now in the Update block I need to convert this data latit,longit, rout_dirc to marker to remove the old one from the map and HashMap before updating its location. How can I do that in my case? How can I remove the old marker from the map since I dont have Marker in the HashMap now also I cant use marker.remove() to delete the old one?
I appreciate any help
Code:
public class Map extends FragmentActivity {
GoogleMap map;
static HashMap<Integer, String> markerMap = new HashMap<Integer, String>();
static String marker_string;
static Marker marker = null;
private void gotoLocation(int id, double lat, double lng,
String route_direct) {
final float zoom = 11;
LatLng ll = null;
if (markerMap.containsKey(id)) {
// Update the location.
marker_string = markerMap.get(id);
String[] marker_string_split = marker_string.split(",");
double latit = Double.parseDouble(marker_string_split[0]);
double longit = Double.parseDouble(marker_string_split[1]);
String rout_dirc = marker_string_split[2];
LatLng LL_2 = new LatLng(lat, lng);
MarkerOptions markerOpt2 = new MarkerOptions().title(route_direct)
.position(LL_2);
// This here doest work.
marker.remove();
// Remove from the HashMap tp add the new one.
markerMap.remove(id);
ll = new LatLng(lat, lng);
MarkerOptions markerOpt = new MarkerOptions().title(route_direct)
.position(ll);
marker = map.addMarker(markerOpt);
String lat1 = Double.toString(lat);
String lng1 = Double.toString(lng);
String data = lat1 + "," + lng1 + "," + route_direct;
markerMap.put(id, data);
zoom();
} else {
// Add a new marker
String lat1 = Double.toString(lat);
String lng1 = Double.toString(lng);
String data = lat1 + "," + lng1 + "," + route_direct;
markerMap.put(id, data);
ll = new LatLng(lat, lng);
MarkerOptions markerOpt = new MarkerOptions().title(route_direct)
.position(ll);
marker = map.addMarker(markerOpt);
zoom();
}
}
}
It seems like your marker in marker.remove(); is set to null and it won't do any good unless you have a reference of the markers you want to delete. If you want to remove all old markers, then you can try the following:
mMap = super.getMap();
map.clear();
This removes everything. Otherwise, when you add markers you should have a reference if you need to delete it later.
For example, your marker variable is:
Marker marker = map.addMarker(..);
Then you can remove it by marker.remove();
Hope this makes sense;
mapView.clear()
it will remove all markers in the map,
you can also refer the google official documentation
https://developers.google.com/maps/documentation/ios-sdk/marker
I am trying to store my location into the parse.com database, but i have having an issue doing so. First of all, I can't store anything into the database I believe it is returning null or something so. Not quite sure whats the behavior. Can someone guide me in the right direction?
double longitude = location.getLongitude();
double latitude = location.getLatitude();
// Get longitude of the current location
// Create a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
ParseObject parkingobject = new ParseObject("Cooking");
parkingobject.put("username","Alana");
ParseGeoPoint point = new ParseGeoPoint(latLng); //<----- This is where the error is.
parkingobject.put("Location", point);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
ParseGeoPoint geoPoint = new ParseGeoPoint(latitude , longitude );
ParseObject parkingobject = new ParseObject("Cooking");
parkingobject.put("username","Alana");
parkingobject.put("Location", geoPoint );
Try this code.
So I'm storing markers from my map into a sqlite database. I am trying to query the markers by latitude and longitude. So for testing I am adding the marker to a specific location. When I check the markers position it gives me another location. What is causing this?
private void drawMarker(LatLng point, String title, String snippet){
// Creating an instance of MarkerOptions
MarkerOptions markerOptions = new MarkerOptions();
// Setting latitude and longitude for the marker
LatLng l = new LatLng(37.52100000000001, -77.44800000000001);
markerOptions.position(l);
markerOptions.title(title);
markerOptions.snippet(snippet);
// Adding marker on the Google Map
googleMap.addMarker(markerOptions);
}
This is the code to extract the marker position
googleMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
marker1 = marker;
LatLng mlatlong = marker1.getPosition();
markerLat = Double.toString(mlatlong.latitude);
markerLng = Double.toString(mlatlong.longitude);
latitude = mlatlong.latitude;
longitude = mlatlong.longitude;
Log.i("Marker", mlatlong.latitude +","+mlatlong.longitude);
markerDialog();
}
});
This gives me the position
06-20 21:53:52.447: I/Marker(15221): 37.52100000569583,-77.44800008833408
In Java, and most other languages you'll come accross, double is not stored as a precise value.
You can read more here.
Also, it shouldn't really matter the locations you posted are extremely close to each other.
I was working with a Google map v2 oriented android project. In my program i retrieves the name,vicinity,latitude&longitude from the places api while touching on the info boxes of the markers.But it every time retrieves the same value.But it shows the correct result in the info boxes.How to solve this bug .Someone please solve this.
Code
for(final Place place : nearPlaces.results){
// Creating a marker
MarkerOptions markerOptions = new MarkerOptions();
// Getting latitude of the place
double latitude = place.geometry.location.lat;
double longitude = place.geometry.location.lng;
// Getting name
String NAME = place.name;
// Getting vicinity
String VICINITY = place.vicinity;
//final String REFERENCE = place.reference;
final String slat = String.valueOf(latitude);
final String slon = String.valueOf(longitude);
LatLng latLng = new LatLng(latitude, longitude);
// Setting the position for the marker
markerOptions.position(latLng);
// Setting the title for the marker.
//This will be displayed on taping the marker
markerOptions.title(NAME + " : " + VICINITY);
markerOptions.icon(bitmapDescriptor);
// Placing a marker on the touched position
mGoogleMap.addMarker(markerOptions);
mGoogleMap.setOnInfoWindowClickListener(
new OnInfoWindowClickListener(){
#Override
public void onInfoWindowClick(Marker arg0) {
// TODO Auto-generated method stub
arg0.hideInfoWindow();
alert.showpickAlertDialog2(PlacesMapActivity.this, slat, slon, place.reference,KEY_TAG);
}
}
);
}
LatLng mylatLng = new LatLng(mylatitude, mylongitude);
// Creating CameraUpdate object for position
CameraUpdate updatePosition = CameraUpdateFactory.newLatLng(mylatLng);
// Creating CameraUpdate object for zoom
CameraUpdate updateZoom = CameraUpdateFactory.zoomBy(10);
// Updating the camera position to the user input latitude and longitude
mGoogleMap.moveCamera(updatePosition);
// Applying zoom to the marker position
mGoogleMap.animateCamera(updateZoom);
What you are doing wrong is setting OnInfoWindowClickListener in a loop.
Take that code outside and retrieve slat and slon from Marker arg0 by using getPosition.
There are other people who are struggling with this issue as well.
You are assuming that the slat and slong values are going to carry through to the onInfoWindowClick() method, but you are always getting the same values there.
Here is a discussion some other folks were having on this topic.