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);
}
}
Related
Not sure what I need to correct to restrict all results from UK..
Presently query returns results from Singapore instead of UK.
I am using API 19.
try {
Geocoder geocoder = new Geocoder(getApplicationContext(),
Locale.UK);
List<Address> results = geocoder.getFromLocationName(toSearch,
1);
if (results.size() == 0) {
return null;
}
address = results.get(0);
GeoPoint p = new GeoPoint((int) (address.getLatitude() * 1E6),
(int) (address.getLongitude() * 1E6));
MapsActivity.this.myMapController.setCenter(p);
List<Overlay> mapOverlays = myMapView.getOverlays();
MapOverlay itemizedoverlay = (MapOverlay) mapOverlays.get(0);
itemizedoverlay.update(p);
} catch (Exception e) {
return null;
}
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(...).
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 want to remove same overlay at the specific position. This is my code for add the overlay, but how can get the position for every overlay?
public void visual_overlay(List<DatiOverlay> list_overlay){
CustomOverlay itemized_overlays = null;
GeoPoint point = null;
for(int ov=0;ov<list_overlay.size();ov++){
itemized_overlays = new CustomOverlay(drawable,mappa, Integer.toString(list_overlay.get(ov).get_id_segnalazione()),list_overlay.get(ov).get_via(),list_overlay.get(ov).get_citta(),list_overlay.get(ov).get_descrizione(),list_overlay.get(ov).get_cek_feed());
point = new GeoPoint((int)(list_overlay.get(ov).get_lat() * 1E6), (int)(list_overlay.get(ov).get_lng() * 1E6));
OverlayItem overlayItem = new OverlayItem(point, list_overlay.get(ov).get_nome(), list_overlay.get(ov).get_prezzo());
itemized_overlays.addOverlay(overlayItem);
mappa.invalidate();
mappa.getOverlays().add(itemized_overlays);
}
You remove an overlay from a MapView like this:
List<Overlay> mapOverlays = mapView.getOverlays();
if (mapOverlay != null) mapOverlays.remove(someOverlay);
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);