i have a googlemap app that displays two items (itemizedoverlay class) and i have a Update button. When i press the update button, i want to remove the items from the map, and put again the same items but with the new latitude and longitude values (i have a service that actualice the latitude and longitude fields that i have in sharedPreferences, and also i recibe the other position by a bundle)
what i am doing wrong? when i try my code, the items of the map doesn't be removed.... just new items appear... but i want to remove the old ones
updateButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mapOverlays.clear();
updateMyPosition();
updateFriendPosition();
}
});
and these are my two functions:
private void updateFriendPosition()
{
Bundle bundle = this.getIntent().getExtras();//get the intent & bundle passed by X
userText.setText(bundle.getString("user"));
permissionText.setText(bundle.getString("permission"));
lastUpdateText.setText(bundle.getString("lastupdate"));
String coordinates[] = {bundle.getString("lat"), bundle.getString("lon")};
lat = Double.parseDouble(coordinates[0]);
lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
OverlayItem overlayitem1 = new OverlayItem(p, bundle.getString("user"), "Hi Friend!");
itemizedoverlay.addOverlay(overlayitem1);
mapOverlays.add(itemizedoverlay);// dibujo la estrella con la posicion actual del friend
mc.animateTo(p); ///nos centra el mapa en la posicion donde esta nuestro amigo
mc.setZoom(10); /// ajusta el zoom a 10
}
private void updateMyPosition()
{
String coordinates[] = {settings.getString("mylatitude", null),settings.getString("mylongitude", null)};
lat = Double.parseDouble(coordinates[0]);
lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
OverlayItem overlayitem1 = new OverlayItem(p, "Me", "My Position");
itemizedoverlay2.addOverlay(overlayitem1);
mapOverlays.add(itemizedoverlay2);//dibujo mi icono para mostrarme a mi
}
i also tryed with invalidate() on the mapview but it doesn't works... still paint the old items...
It looks like this is based on the SDK tutorial. You've added two things to itemizedoverlay and the other one. You need to add a method in HelloItemizedOverlay, namely 'clear'
public void clear() {
mOverlays.clear();
}
Call this method before you call
itemizedoverlay.addOverlay(overlayitem1);
Related
I want display Google maps in the Android Based on the user edit text value lat and lang and place name and then i finally click on the search button the maps was displayed in Android.
you need to create some object like.
MapView mapView;
MapController mc;
GeoPoint p;
String add = "";
double lattitude ;
double longitude;
Searching location by Name and show Maps.
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Search Location");
alert.setMessage("Enter Location Name: ");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
// Do something with value!
Log.d("value", value);
Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocationName(
value, 5);
String add = "";
if (addresses.size() > 0) {
p = new GeoPoint(
(int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));
mc.animateTo(p);
mapView.invalidate();
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
Searching location by Entering Latitude and Longitude and show map..
searchUsingLangLat()
{
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.latlong, null);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Search Location");
alert.setMessage("Enter Lattitude and Longitude: ");
alert.setView(textEntryView);
// Set an EditText view to get user input
AlertDialog latLongPrompt = alert.create();
final EditText lat = (EditText) textEntryView.findViewById(R.id.lat);
final EditText longi = (EditText) textEntryView.findViewById(R.id.longi);
//alert.setView(lat);
//alert.setView(longi);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Toast.makeText(getBaseContext(), "clicked ok ", Toast.LENGTH_SHORT).show();
Double value1 = Double.parseDouble(lat.getText().toString());
Double value2 = Double.parseDouble(longi.getText().toString());
// Do something with value!
// Log.d("value1", value1);
//Log.d("value2", value2);
p = new GeoPoint(
(int) (value1 * 1E6),
(int) (value2 * 1E6));
mc.animateTo(p);
mc.setZoom(17);
mapView.invalidate();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
And finally you need to show map. write following code in onCreate() method.
mapView = (MapView) findViewById(R.id.mapView);
LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);
View zoomView = mapView.getZoomControls();
zoomLayout.addView(zoomView,
new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mapView.displayZoomControls(true);
mc = mapView.getController();
String coordinates[] = {"1.352566007", "103.78921587"};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
mc.setCenter(p);
mc.setZoom(17);
//---Add a location marker---
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);
Try this code...if you get any error free to ask.
If you store the Latitude and Longitude of a bunch of locations in a SQLite Database, You could retrieve these values and place them each in an OverlayItem class for use in Google's Map code!!
Database name: database
Table name: place
Fields in place Table:
id
title
description
latitude
longitude
You would want to do a query like this:
SELECT title, description, latitude, longitude
FROM place
Which can be done in Android like this:
// get your database object here using your DbHelper object and the
// method getReadableDatabase();
ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
Cursor locationCursor = databaseObject.query("place", new String[] {
"title", "description", "latitude", "longitude" }, null, null,
null, null, null);
locationCursor.moveToFirst();
do {
String title = locationCursor.String(locationCursor
.getColumnIndex("title"));
String description = locationCursor.String(locationCursor
.getColumnIndex("description"));
int latitude = (int) (locationCursor.getDouble(locationCursor
.getColumnIndex("latitude")) * 1E6);
int longitude = (int) (locationCursor.getDouble(locationCursor
.getColumnIndex("longitude")) * 1E6);
items.add(new OverlayItem(new GeoPoint(latitude, longitude), title,
description));
} while (locationCursor.moveToNext());
You need to times the double values by 1e6 because Android uses an integer representation of the lat/long values. If you already took care of this when populating the database, skip the multiplication and use locationCursor.getInt(...).
So first I checked if there is a Goecoder service present in or not....
Then I use this service and check the result from getFromLocationName, and then i make the point of it which I add to my overlay and display it...But for some reason, it does not display anything.
private void convert_Points() throws IOException {
// initialization of overlays
mapOverlays = mapV.getOverlays();
drawable = this.getResources().getDrawable(R.drawable.pin);
itemizedoverlay = new ItemizedOverlay(drawable);
// check if getFromLocationName is present or not
if (!Geocoder.isPresent()){
Toast.makeText(getBaseContext(),"Sorry! Geocoder service not Present.", Toast.LENGTH_LONG).show();
}
else{
// make OverlayItem by creating a GeoPoint that defines our map coordinates
Geocoder geocoder = new Geocoder(this);
List<Address> geoResults = geocoder.getFromLocationName("Empire State Building", 5,-44.00, 111.00, -12.0, 155.0);
Address location = geoResults.get(0);
location.getLatitude();
location.getLongitude();
String s=Double.toString(geoResults.get(0).getLatitude());
Toast.makeText(getBaseContext(),s, Toast.LENGTH_LONG).show();
GeoPoint point= new GeoPoint( (int) (location.getLatitude() * 1E6), (int) (location.getLongitude() * 1E6));
OverlayItem overlayitem = new OverlayItem(point, "", "");
// add this OverlayItem to a collection in the ItemizedOverlay
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
}
I have a for loop, inside which I'm calling a method to show the poi's in map. Now what I am trying to do is to show each poi in a sequence. I mean now the for loop completes at a stretch and at last is presented with the final output.
But I want it to be in order, that mean each time for loop iterates I should be able to see the animation where these geopoints are moving from place to place.
Here is my code,
for (i=currentPOIindex;i<arraypoi.size();i++) {
poi = arraypoi.get(i);
latitude = poi.getLatitude().toString();
longitude = poi.getLongitude().toString();
placename = poi.getPlaceName().toString();
mapdescription = poi.getPoiDecsription().toString();
lat = Double.parseDouble(latitude);
lng = Double.parseDouble(longitude);
//use the poiIndex to identify which poi is highlighted
itemizedOverlay.poiIndex = i;
geopoint = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
OverlayItem overlayitem = new OverlayItem(geopoint,fitTextToMapCallout(placename), fitTextToMapCalloutDescription("hello"));
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
//this method is meant to move to each geopint one by one.
//And I am trying to give a delay before this method is called
moveToPOI(i);
}
public void moveToPOI(int currentPOI) {
try {
AudioMapOverLay overlay = (AudioMapOverLay) mapOverlays.get(currentPOI);
poi = arraypoi.get(currentPOI);
latitude = poi.getLatitude().toString();
longitude = poi.getLongitude().toString();
placename = poi.getPlaceName().toString();
lat = Double.parseDouble(latitude);
lng = Double.parseDouble(longitude);
geopoint = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
overlay.onForwardOrRewind(currentPOI, overlay);
} catch (Exception e) {
e.printStackTrace();
}
}
I tried using Thread.sleep(long ms); But it is not the solution, can any one tell me what to do?
You Should have to do some thing like this in your For Loop:
final Handler handler = new Handler();
for (i=currentPOIindex;i<arraypoi.size();i++) {
poi = arraypoi.get(i);
latitude = poi.getLatitude().toString();
longitude = poi.getLongitude().toString();
placename = poi.getPlaceName().toString();
mapdescription = poi.getPoiDecsription().toString();
lat = Double.parseDouble(latitude);
lng = Double.parseDouble(longitude);
//use the poiIndex to identify which poi is highlighted
itemizedOverlay.poiIndex = i;
geopoint = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
OverlayItem overlayitem = new OverlayItem(geopoint,fitTextToMapCallout(placename), fitTextToMapCalloutDescription("hello"));
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
//this method is meant to move to each geopint one by one.
//And I am trying to give a delay before this method is called
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 100ms
moveToPOI(i);
}
}, 100/*This is Delay Value in Milli Second set which suit for your /*);
}
You could use Handler postDelayed and Runnable which does the actual operation, inside Runnable run() you remove callbacks for that runnable and post it again to the handler. If you are getting some callbacks for the operation finished you can use that to trigger the runnable again.
Handler handler = new Handler()
handler.postDelayed(myRunnable, MY_DELAY);
Runnable myRunnable = new Runnable() {
public void run() {
//do the stuff
// Check if another loop sequence is needed and start runnable again
handler.removeCallbacks(myRunnable);
handler.postDelayed(myRunnable, MY_DELAY); }
Finally I have come up with the answer. #Niko and #Herry, I don't know if this is similar to your code.
here is my change,
notification = new Runnable() {
public void run() {
testgeo();
}
};
handler.postDelayed(notification, 1500);
where testge0() is,
private void testgeo() {
poi = arraypoi.get(index);
latitude = poi.getLatitude().toString();
longitude = poi.getLongitude().toString();
placename = poi.getPlaceName().toString();
// mapdescription = poi.getPoiDecsription().toString();
lat = Double.parseDouble(latitude);
lng = Double.parseDouble(longitude);
if (index == currentPOIindex) {
drawable = this.getResources().getDrawable(R.drawable.poiiconactive);
itemizedOverlay = new AudioMapOverLay(drawable, objMapView,this);
} else {
drawable = this.getResources().getDrawable(R.drawable.mapicon);
itemizedOverlay = new AudioMapOverLay(drawable, objMapView,this);
}
itemizedOverlay.poiIndex = index;
geopoint = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
OverlayItem overlayitem = new OverlayItem(geopoint,fitTextToMapCallout(placename),
fitTextToMapCalloutDescription("hello"));
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
moveToPOI(index);
}
But thank you for your help. This is working for me
I am currently try to retrieve a latitude and longitude value from a location. When i convert the location to integer values using the following code:
LocationManager locMan;
Location location;
String towers;
private static double lat;
private static double lon;
locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria crit = new Criteria();
towers = locMan.getBestProvider(crit, false);
location = locMan.getLastKnownLocation(towers);
if (location != null)
{
lat = (int) (location.getLatitude() * 1E6);
lon = (int) (location.getLongitude() * 1E6);
GeoPoint ourLocation = new GeoPoint(lati, longi);
OverlayItem overlayItem = new OverlayItem(ourLocation, "1st String", "2nd String");
CustomPinpoint custom = new CustomPinpoint(d, MainMap.this);
custom.insertPinpoint(overlayItem);
overlayList.add(custom);
overlayList.clear();
lat = (double) lat;
lon = (double) lon;
System.out.println("Lat is " + lat);
System.out.println("Longi is " + lon);
}
else
{
System.out.println("Location is null! " + towers);
Toast.makeText(MainMap.this, "Couldn't get provider", Toast.LENGTH_SHORT).show();
}
it comes back in the format of 0.000000
lat is 5.494394
long is -7.724457
how can i get it back in the format 00.000000
I have tried DecimalFormat, Math.Round and various other solutions i found on Stack Overflow but still get the same result. Please help!
Have you tried this:
DecimalFormat sf = new DecimalFormat("00.000000");
String s = sf.format(5.494394);
System.out.println(s); //prints 05.494394
EDIT
Based on your new question, why don't you do this:
double latitude = location.getLatitude();
double longitude = location.getLongitude();
GeoPoint ourLocation = new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));
//....
System.out.println("Lat is " + latitude);
System.out.println("Longi is " + longitude);
Convert to String, add leading zero.
StringFormatter might help.
An integer will never have leading zeroes.
You do a confusion between "real data" and "representation"
5.494394 is the "real data", that's an integer inferior to 10, it's logical to haven't a decade when you display it directly.
I you want to display every time the decade, also there is equal to 0, you have to test if your integer are inferior to 10 are not.
With an atomic test, this can be done with this way in java:
(lat < 10) ? "0"+lat : lat;
with this function, you are always the decade displayed before the "real data".
public String formatFigureToTwoPlaces(double value) {
DecimalFormat myFormatter = new DecimalFormat("00.00");
return myFormatter.format(value);
}
I had shown latitudes and longitudes in Google map when i click marker it display a balloon on which gallery name and address are written
I want when I click on balloon that gallery name are fetched how to achieve this below is my code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.map);
mapView.setBuiltInZoomControls(true);
mapOverlays = mapView.getOverlays();
drawable1 = getResources().getDrawable(R.drawable.greenballon);
itemizedOverlay = new MyItemizedOverlay(drawable1, mapView);
drawable2 = getResources().getDrawable(R.drawable.blueballon);
itemizedOverlay2 = new MyItemizedOverlay(drawable2, mapView);
drawable3 = getResources().getDrawable(R.drawable.redballon);
itemizedOverlay3 = new MyItemizedOverlay(drawable3, mapView);
for(int i=0;i<20;i++)
{
if(sitesList.getType().get(i).equalsIgnoreCase("Centros Culturales"))
{
name = sitesList.getLatitude().get(i);
name1 = sitesList.getLongitude().get(i);
Log.i("centos culturales lat" + i,name);
Log.i("culturarls longitiitude"+i,name1);
point = new GeoPoint((int) (Double.parseDouble(name) * 1E6),
(int) (Double.parseDouble(name1) * 1E6));
OverlayItem overlayItem = new OverlayItem(point, sitesList.getGalleryname().get(i),
sitesList.getAddress().get(i));
itemizedOverlay.addOverlay(overlayItem);
}
}
for(int i=0;i<sitesList.getLatitude().size();i++)
{
if(sitesList.getType().get(i).equalsIgnoreCase("Centros Culturales"))
{
name = sitesList.getLatitude().get(i);
name1 = sitesList.getLongitude().get(i);
Log.i("centos culturales lat" + i,name);
Log.i("culturarls longitiitude"+i,name1);
point = new GeoPoint((int) (Double.parseDouble(name) * 1E6),
(int) (Double.parseDouble(name1) * 1E6));
OverlayItem overlayItem = new OverlayItem(point, sitesList.getGalleryname().get(i),
sitesList.getAddress().get(i));
itemizedOverlay.addOverlay(overlayItem);
}
}
for(int i=0;i<sitesList.getLatitude().size();i++)
{
if(sitesList.getType().get(i).equalsIgnoreCase("Residencias"))
{
name = sitesList.getLatitude().get(i);
name1 = sitesList.getLongitude().get(i);
Log.i("residencias latitute" + i,name);
Log.i("residencias longitiitude"+i,name1);
point3 = new GeoPoint((int) (Double.parseDouble(name) * 1E6),
(int) (Double.parseDouble(name1) * 1E6));
OverlayItem overlayItem = new OverlayItem(point3, sitesList.getGalleryname().get(i),
sitesList.getAddress().get(i));
itemizedOverlay2.addOverlay(overlayItem);
}
}
mapOverlays.add(itemizedOverlay);
mapOverlays.add(itemizedOverlay2);
**//actuaaly i show lat long in google map according to "gallery type"..so here how to get data on tap of baloon...??????**
#Override
protected boolean onBalloonTap(int index, OverlayItem item) {
for(int i=0;i<sitesList.getLatitude().size();i++)
{
if(sitesList.getType().get(i).equalsIgnoreCase("Centros Culturales"))
{
array_galleryname1 = new String[sitesList.getGalleryname().size()];
array_galleryurl1 = new String[sitesList.getImagesurl().size()];
galleryname = sitesList.getGalleryname().get(i);
galleryurl = sitesList.getImagesurl().get(i);
array_galleryname1[index] = galleryname;
array_galleryurl1[index] = galleryurl;
}
}
Toast.makeText(c, array_galleryname1[index] + index,
Toast.LENGTH_LONG).show();
for(int i=0;i<sitesList.getLatitude().size();i++)
{ if(sitesList.getType().get(i).equalsIgnoreCase("Residencias"))
{
array_galleryname = new String[sitesList.getGalleryname().size()];
array_galleryurl1 = new String[sitesList.getImagesurl().size()];
galleryname = sitesList.getGalleryname().get(i);
galleryurl = sitesList.getImagesurl().get(i);
array_galleryname[index] = galleryname;
array_galleryurl1[index] = galleryurl;
}
}
You have to override onTap() .
See this link. http://developer.android.com/resources/tutorials/views/hello-mapview.html