when you go to Google maps in the browser you can type a country name and the map focuses on that country. Is it possible to get this on a MapView? I'd like my MapView to initially focus on user's country if a location is not available.
EDIT: here's what I have so far:
private MapView mvClear;
private MyLocationOverlay compass;
private MapController controller;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clear_map);
mvClear = (MapView) findViewById(R.id.mvClear);
mvClear.setBuiltInZoomControls(true);
TouchOverlay touchOverlay = new TouchOverlay();
List<Overlay> overlays = mvClear.getOverlays();
overlays.add(touchOverlay);
compass = new MyLocationOverlay(this, mvClear);
overlays.add(compass);
controller = mvClear.getController();
controller.setZoom(8);
Geocoder coder = new Geocoder(this);
try {
List<Address> address = coder.getFromLocationName(Locale.getDefault().getCountry(), 1);
int lat = (int) address.get(0).getLatitude();
int lon = (int) address.get(0).getLongitude();
GeoPoint point = new GeoPoint(lat, lon);
controller.setCenter(point);
} catch (IOException e) {
e.printStackTrace();
}
}
No focus.
Best solution I found was to save the default center point as String resource so it can be "translated" if the app is localized. That's it for now..
Related
I am using osmdroid and i am just having an issue with getting the coordinates of my initial location on the map.
I can get the location on the map and i can get the initial location to print inside the runable.
Is there any way i can get just the initial coordinates? I dont need to update them or anything..
I just need them so i can design a route between the initial location and the destination....
Any help would be great thanks!
Here is my code;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Context ctx = getApplicationContext();
//important! set your user agent to prevent getting banned from the osm servers
Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx));
setContentView(R.layout.activity_map);
mMapView = (MapView) findViewById(R.id.mapview);
mMapView.setTileSource(TileSourceFactory.MAPNIK);
mMapView.setBuiltInZoomControls(true);
mMapView.setMultiTouchControls(true);
IMapController mapController = mMapView.getController();
mapController.setZoom(13);
GeoPoint startPoint = new GeoPoint(52.167247, -6.888583);
Marker POI1 = new Marker(mMapView);
Marker POI2 = new Marker(mMapView);
Marker POI3 = new Marker(mMapView);
Marker POI4 = new Marker(mMapView);
POI1.setPosition(new GeoPoint(52.187668, -6.866953));
POI2.setPosition(new GeoPoint(52.124359, -6.926442));
POI3.setPosition(new GeoPoint(52.222310, -6.933613));
POI4.setPosition(new GeoPoint(52.221245, -6.830961));
List<OverlayItem> markers = null;
mMapView.getOverlays().add(POI1);
mMapView.getOverlays().add(POI2);
mMapView.getOverlays().add(POI3);
mMapView.getOverlays().add(POI4);
mapController.setCenter(startPoint);
//add
GpsMyLocationProvider provider = new GpsMyLocationProvider(this);
provider.addLocationSource(LocationManager.NETWORK_PROVIDER);
locationOverlay = new MyLocationNewOverlay(provider, mMapView);
locationOverlay.enableMyLocation();
locationOverlay.enableFollowLocation();
locationOverlay.runOnFirstFix(new Runnable() {
public void run() {
Log.d("MyTag", String.format("First location fix: %s", locationOverlay.getLastFix()));
}
});
mMapView.getOverlayManager().add(locationOverlay);
//System.out.println("hello joe: " + currentLocation.getLongitude() );
RoadManager roadManager = new OSRMRoadManager(this);
ArrayList<GeoPoint> wayPoints = new ArrayList<GeoPoint>();
wayPoints.add(startPoint);
GeoPoint endPoint = new GeoPoint(52.221245, -6.830961);
wayPoints.add(endPoint);
Road road = roadManager.getRoad(wayPoints);
if (road.mStatus != Road.STATUS_OK){
Toast.makeText(this, "Error when loading road", Toast.LENGTH_LONG).show();
}
Polyline roadOverlay = RoadManager.buildRoadOverlay(road);
mMapView.getOverlays().add(roadOverlay);
}
I solved this by using the location provider
I am trying to buid a simple app that when user enter his desired location its map appear.But i am getting an error Cannot instantiate the type GeoPoint i have also installed Google Play Services.
here is the code :
public class MainActivity extends MapActivity {
EditText location;
Geocoder geoCoder;
GeoPoint p;
MapController controller;
MapView mapView;
Button btnSearch;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
location=(EditText)findViewById(R.id.txtAddress);
mapView = (MapView) findViewById(R.id.mapView);
btnSearch.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
List<Address> addresses;
try {
addresses = geoCoder.getFromLocationName(location.getText().toString(),1);
if(addresses.size() > 0)
{
p = new GeoPoint( (int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));
controller.animateTo(p);
controller.setZoom(12);
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
location.setText("");
}
else
{
AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
adb.setTitle("Google Map");
adb.setMessage("Please Provide the Proper Place");
adb.setPositiveButton("Close",null);
adb.show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
Is there a particular reason you need to use v1 of the Google maps api, instead of the current v2? If not, try using LatLng to store the location, and map.moveCamera(LatLng, float zoom) to go to the desired location as shown here.
I am a beginner of Android Developer. I am developing the Map Application. I have a function of searching address but I do not know how to search address by name using Google Map API V.2. Please suggest me the solution and some code to solve this. Thank you very much and Sorry with my English.
adderess = c.getString(c.getColumnIndex(SQLiteAdapter.KEY_CONTENT3));
// get address in string for used location for the map
/* get latitude and longitude from the adderress */
Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
try
{
List<Address> addresses = geoCoder.getFromLocationName(adderess, 5);
if (addresses.size() > 0)
{
Double lat = (double) (addresses.get(0).getLatitude());
Double lon = (double) (addresses.get(0).getLongitude());
Log.d("lat-long", "" + lat + "......." + lon);
final LatLng user = new LatLng(lat, lon);
/*used marker for show the location */
Marker hamburg = map.addMarker(new MarkerOptions()
.position(user)
.title(adderess)
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.marker)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(user, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
}
catch (IOException e)
{
e.printStackTrace();
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_map);
editText = (EditText) findViewById(R.id.editText1);
mapView = (MapView) findViewById(R.id.mapView);
btngo = (Button) findViewById(R.id.btnmapsites);
btngo.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
List<Address> addresses;
try{
addresses = geoCoder.getFromLocationName(editText.getText().toString(),1);
if(addresses.size() > 0){
p = new GeoPoint( (int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));
controller.animateTo(p);
controller.setZoom(12);
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
editText.setText("");
}else{
AlertDialog.Builder adb = new AlertDialog.Builder(SearchMapActivity.this);
adb.setTitle("Google Map");
adb.setMessage("Please Provide the Proper Place");
adb.setPositiveButton("Close",null);
adb.show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
One part of the application I am making requires google map. I want the user to be able to type into edittext some search string and then press a search button and the map will animate to the first result.
When I press the button the application searches and takes the first result and puts it into a geopoint, so that part of the program is working. But when I try to animate to that point the application crashes.
Here is the onCreate function where I successfully navigate to the location "Dalvík, Iceland".
public class LocationPicker extends MapActivity {
static GeoPoint point;
static MapController mc;
static MapView mapView;
private EditText location;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location_picker);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
location = (EditText)findViewById(R.id.locationString);
mc = mapView.getController();
String tmp = "Dalvík, Iceland";
try {
point = searchLocation(tmp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mc.animateTo(point);
mc.setZoom(14);
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
}
And my code for the button click is, and to be clear the System.out.println(point) prints valid point. But still when I click the button my application crashes.
public void search(View v) throws IOException{
GeoPoint tmpPoint = searchLocation(location.getText().toString());
System.out.println(tmpPoint);
if( tmpPoint != null){
mc.animateTo(tmpPoint);
mapView.invalidate();
}
}
And the searchLocation functions is as follows:
public GeoPoint searchLocation (String searchString) throws IOException{
Geocoder geo = new Geocoder(this);
List<Address> addr;
addr = geo.getFromLocationName(searchString, 10);
if(!addr.isEmpty()){
Address loc = addr.get(0);
GeoPoint point = new GeoPoint((int) (loc.getLatitude() * 1E6),
(int) (loc.getLongitude() * 1E6));
return point;
}
else {
return null;
}
}
So to summerize I am clearly doing something wrong in the onclick "search" function.
Any ideas what is wrong?
You are not checking the right variable for null value:
if( point != null){
mc.animateTo(tmpPoint);
mapView.invalidate();
}
should be
if( tmpPoint!= null){
mc.animateTo(tmpPoint);
mapView.invalidate();
}
Additionnally, you want to make the call to the geoCoder.getFromLocationName method outside the UI thread as this can be time consumming. Use an AsyncTask for that for instance.
Finally, even if you get a list of addresses, they are not guaranted to have latitude and longitude. Use the hasLatitude and hasLongitude functions to pick the first address of the result list with coordinates.
I found the answer.
The problem causing this was this line
mapView.invalidate();
I have a mapview page that displays the current location in the map.
I am using MyLocationOverlay for the purpose. The code for that goes as follows:
myLocationOverlay = new MyLocationOverlay(this, mapView);
myLocationOverlay.enableCompass();
myLocationOverlay.enableMyLocation();
myLocationOverlay.runOnFirstFix(new Runnable() {
public void run() {
geopoint = myLocationOverlay.getMyLocation();
ItemizedOverlay itemizedoverlay = new ItemizedOverlay(drawable);
try{
overlayitem = new OverlayItem(geopoint, "", "");
}
catch(Exception e){
e.printStackTrace();
}
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
mapController.animateTo(geoPoint);
}
});
The above code works and gets the current location through gps and sets a marker at that point.
But the code inside try block is the issue.
I am getting null value inside geopoint.
Actually I have to get that point and do some calculations with distance and all. For that I have to get the correct value inside that geopoint.
Can anyone please tell the solution for this?
I've been working on the same problem for a day now and here's what I've discovered. For some reason this works
LocationManager lm;
GeoPoint gp;
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location lastKnownLoc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (lastKnownLoc != null){
int longTemp = (int)(lastKnownLoc.getLongitude()* 1000000);
int latTemp = (int)(lastKnownLoc.getLatitude() * 1000000);
gp = new GeoPoint(latTemp, longTemp);
}
and this doesn't
gp = myLocOverlay.getMyLocation();
Tracking current location can be done by using MyLocationOverlay class and through LocationManager. Using MyLocationOverlay, the code below works..
MyLocationOverlay myLocationOverlay;
MapView mapView;
MapController mapcontroller;
OverlayItem overlayitem;
List<Overlay> mapoverlays;
GeoPoint geopoint;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.currentlocation);
mapView = (MapView) findViewById(R.id.map);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(false);
mapView.setStreetView(true);
mapView.setTraffic(true);
mapcontroller = mapView.getController();
mapoverlays = mapView.getOverlays();
drawable = this.getResources().getDrawable(R.drawable.ic_map_red);
mapView.invalidate();
getCurrentLocation();
}
public void getCurrentLocation(){
myLocationOverlay = new MyLocationOverlay(this, mapView);
myLocationOverlay.enableCompass();
myLocationOverlay.enableMyLocation();
myLocationOverlay.runOnFirstFix(new Runnable() {
public void run() {
ItemizedOverlay itemizedoverlay = new ItemizedOverlay(drawable);
try {
latitude = myLocationOverlay.getMyLocation().getLatitudeE6();
longitude = myLocationOverlay.getMyLocation().getLongitudeE6();
geopoint = new GeoPoint((int) (latitude), (int) (longitude));
overlayitem = new OverlayItem(geopoint, "", "");
itemizedoverlay.addOverlay(overlayitem);
mapoverlays.add(itemizedoverlay);
mapcontroller.animateTo(geopoint);
mapcontroller.setZoom(16);
}
catch (Exception e) {}
}
});
}
If you override:
onLocationChanged();
Remember to call:
super.onLocationChanged();
after or before your own code. This appears to ensure that the variable
getMyLocation()
returns is not null.
The friend upstairs made me rise that try override nothing, neither draw nor drawMyLocation.
Just like this:
MyLocationOverlay myLocationOverlay = new SelfLocationOverlay(this, mapView);
myLocationOverlay.enableMyLocation();
myLocationOverlay.disableCompass();
mapView.getOverlays().add(myLocationOverlay);