How to use Latitud and Longitud as a variable in Android? - android

I have an SQLITE3 database where I defined lat and long as text.
I need to use those lat, and long as the final destination in a map.
The intent is defined as:
if(locationMap != null){
Intent theIntent = new Intent(getApplication(), displayMap.class);
theIntent.putExtra("_Id", locationMap.get("_Id"));
theIntent.putExtra("locCode", locationMap.get("locCode"));
theIntent.putExtra("locDesc", locationMap.get("locDesc"));
theIntent.putExtra("locLat", locationMap.get("locLat"));
theIntent.putExtra("locLong", locationMap.get("locLong"));
theIntent.putExtra("locTelephone", locationMap.get("locTelephone"));
theIntent.putExtra("locComments", locationMap.get("locComments"));
startActivity(theIntent); // display map with coordinates
}
In the next activity I recover the values in the On create method:
// Parameters
String locCode = i.getStringExtra("locCode");
String locDesc = i.getStringExtra("locDesc");
String locLat = i.getStringExtra("locLat");
String locLong = i.getStringExtra("locLong");
String locTelephone = i.getStringExtra("locTelephone");
String locComments = i.getStringExtra("locComments");
String Text = "Current location is: " +
i.getStringExtra("locLat");
Toast.makeText( getApplicationContext(),Text,
Toast.LENGTH_SHORT).show();
System.out.println("locCode: " + locCode);
System.out.println("LocDesc: " + locDesc);
System.out.println("LocLat: " + locLat);
System.out.println("LocLong: " + locLong);
System.out.println("LocTelephone: " + locTelephone);
System.out.println("LocComment: " + locComments);
getLocation(ORIGIN);
setContentView(R.layout.map);
if (mLastSelectedMarker != null && mLastSelectedMarker.isInfoWindowShown()) {
// Refresh the info window when the info window's content has changed.
mLastSelectedMarker.showInfoWindow();
}
setUpMapIfNeeded();
}
I need to use those locLat and Loclong instead of the numbers:
public class displayMap extends FragmentActivity implements
OnMarkerClickListener,
OnInfoWindowClickListener {
public LatLng ORIGIN = new LatLng(34.02143074239393, -117.61349469423294);
public LatLng DESTINY = new LatLng(34.022365269080886, -117.61271852999926);
private GoogleMap mMap;
private Marker mDestiny;
private Marker mOrigin;
private Marker mLastSelectedMarker; // keeps track of last selected marker
I've tried transforming the text to double and It won't allow me to.
I've tried many solutions I found on stack overflow, but no luck yet.
I appreciate any help
Thanks in advance.

You need to parse the latitude and longitude from String to double to use in new LatLng();
double latitude = Double.parseDouble(locLat);
double longitude = Double.parseDouble(locLong);
and then,
public LatLng ORIGIN = new LatLng(latitude, longitude);

you need cast them into double. As GPRathour says.

Change the type of Lat Long Text to REAL in your SQL Lite,
when inserting values use this
values.put(Latitude_Column, ORIGIN.latitude);
values.put(Longitude__Column,ORIGIN.longitude);
And for retrieving values
LatLng origin = new LatLng(cursor.getDouble(cursor.getColumnIndex(Latitude_Column)),cursor.getDouble(cursor.getColumnIndex(Longitude__Column)));
No need to parsing values

Related

.cameraPosition.target does not provide center of current view

I have a location marked in google map. The user has the option to change this location by dragging the map. I am using camera position to get the new location.Here is my code to get the original location:
mMap = googleMap
val addkey = intent.getStringExtra("address")
var addlocation = getLocationFromAddress(addkey) as LatLng
var mapLocation = CameraUpdateFactory.newLatLngZoom(addlocation, 18.0f)
mMap.animateCamera(mapLocation)
The getLocationFromAddress code is working fine.
The user then drags to a new location, clicks a button when done and upon confirmation the new location is accepted. I am trying to get the Latitude Longitude of the current camera position using the single line code:
addlocation = mMap.cameraPosition.target
But the code continues to return the Lat Long of old view. Where am I wrong?
Implement it like this -
private GoogleMap.OnCameraIdleListener onCameraIdleListener;
then use the camera listener to get the latitude longitude from the camera center position like this -
onCameraIdleListener = new GoogleMap.OnCameraIdleListener() {
#Override
public void onCameraIdle() {
LatLng latLng = mMap.getCameraPosition().target;
Geocoder geocoder = new Geocoder(MapsActivity.this);
try {
List<Address> addressList = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);
if (addressList != null && addressList.size() > 0) {
String locality = addressList.get(0).getAddressLine(0);
String country = addressList.get(0).getCountryName();
if (!locality.isEmpty() && !country.isEmpty())
resutText.setText(locality + " " + country);
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
Getting the address is an addition thing added here. Use it if you need it.

Convert String to Marker in Google Map to remove the old Marker in the Map

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

Google places retrieves same value in all the execution

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.

Converting String to Double for use in LatLng/Google Maps

I need to convert a String value into a LatLng value for use in a GoogleMaps fragment in an Android app. The string value will likely come in the form of "-45.654765, 65.432892".
I've tried two different ways of doing this, and both have resulted in errors. First, I've tried using split() and putting the results into a String[], then accessing each using parseDouble(), as follows:
String[] geo = GEO.split(",");
double lati = Double.parseDouble(geo[0]);
double lngi = Double.parseDouble(geo[1]);
LOCATION = new LatLng(lati, lngi);
This yields an ArrayIndexOutOfBoundsException caused by double lati = Double.parseDouble(geo[0]);. I'm not really sure why.
I've also tried using StringTokenizer, as follows:
StringTokenizer tokens = new StringTokenizer(GEO, ",");
String lat = tokens.nextToken();
String lng = tokens.nextToken();
double lati = Double.parseDouble(lat);
double lngi = Double.parseDouble(lng);
LOCATION = new LatLng(lati, lngi);
This yields a NoSuchElementException pointing to String lng = tokens.nextToken();.
In both cases, the String I am working on, GEO, is public static final and passed from another activity via intent, where it is currently just hardcoded as "43.75,-70.15".
LOCATION is public static and is a LatLng variable initialized as null.
Can anyone point me in the right direction? This seems really simple so I'm even more confused than usual...
EDIT:
The data originates in a different activity where it is passed via intent. The activity that receives the intent has GEO defined as follows:
public static final String GEO = "geo";
And the intent from the previous activity puts geo in like this:
bundle.putString(PlaceActivity.GEO, geo);
You should have
String loc = getIntent().getExtras().getString(PlaceActivity.GEO);
String[] geo = loc.split(",");
You could just use a very basic substring:
int index = GEO.indexOf(",");
String lat = GEO.substring(0, index).trim();
String lng = GEO.substring(index+1).trim();
double lati = Double.parseDouble(lat);
double lngi = Double.parseDouble(lng);
LOCATION = new LatLng(lati, lngi);
Sorry, untested.

Passing current location ( longitude and latitude ) in the Google Places API

am trying to integrate Google Places API into my app.
Now I am finding out my current location of the Phone, how do I embed the longitude and latitude which I have got into the following URL instead of "location=34.0522222,-118.2427778"
"https://maps.googleapis.com/maps/api/place/search/xml?location=34.0522222,-118.2427778&radius=500&types=restaurants&sensor=false&key=Your_API_Key"
Do you mean how do you do string manipulation such as (untested):
int latitude = ...;
int longitude = ...;
String preamble = "https://maps.googleapis.com/maps/api/place/search/xml?location=";
String postamble = "&radius=500&types=restaurants&sensor=true&key=";
String key = "Your_api_key";
String latStr = latitude + "";
String longStr = longitude + "";
String url = preamble + latStr + "," + longStr + postamble + key;
$lat = location[0];
$long = location[1];
Should work, but I use json for geting long and lat from google. Is better. I can check it again if not working.
Here is better json solution:
http://maps.googleapis.com/maps/api/geocode/json?address=Atlantis&sensor=true&oe=utf-8
You should change Atlantis to address

Categories

Resources