I am making an app in android to get location name in google map.i have to get the particular location name when i tap in a pariticular location in my google map.i get error when i use my code , anyone can explain why it happens!!!
package com.android.mapadress;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.os.IBinder;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;
class MapOverlay extends com.google.android.maps.Overlay
{
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
//---when user lifts his finger---
if (event.getAction() == 1) {
GeoPoint p = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
Toast.makeText(this,
p.getLatitudeE6() / 1E6 + "," +
p.getLongitudeE6() /1E6 ,
Toast.LENGTH_SHORT).show();
}
return false;
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
public class mapadress extends MapActivity {
/** Called when the activity is first created. */
//@Override
private MapOverlay overlay;
开发者_如何学Python LinearLayout linearLayout;
MapView mapView;
MotionEvent event;
//@Override
protected boolean isRouteDisplayed()
{
return false;
}
public void onCreate(Bundle savedInstanceState) {
try
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
overlay.onTouchEvent(event, mapView);
}
catch(Exception e)
{
}
}
}
Use ItemizedOverlay class to represent markers... Tutorial on android developer site describes it very clearly.
There are two versions of OnTap method. One gives location, you can use that.
There is a google web service for reverse geocoding (Given below, use your own latitude and longitude here, to change output in json you can change &output=json
instead of &output=xml
), there must be a geocoding class but unfortunately I'm not aware of this.
http://maps.google.com/maps/geo?q=" + latitude + ","+ longitude + "&output=xml
Fetch the value of proper node (xml) or proper key (json) to get the desired value.
精彩评论