我目前正在研究如何在我的 android 应用程序中使用 google api 将地点添加到 google 地图。我可以使用 google api 自动完成位置,也可以让反向地理编码工作。我编写了添加地点的代码,但是当我在我的应用程序中添加位置时,未添加图钉。我不知道我的错误在哪里。但我想是我的 google api url 有问题。
下面是我的代码:
class addPlaces extends AsyncTask<String, String, PlacesList> {
@Override
protected PlacesList doInBackground(String... arg0) {
try {
Log.v(LOG_KEY, "Adding Place...");
GenericUrl reqUrl = new GenericUrl(PLACE_ADD_URL);
reqUrl.put("key", API_KEY);
reqUrl.put("sensor", "false");
Log.v(LOG_KEY, "Adding Place...");
// reqUrl.put("Host:", "maps.googleapis.com");
/*
* reqUrl.put( "Host: maps.googleapis.com",
* "{\"location\":{\"lat\":" + Double.toString(lat) +
* ",\"lng\":" + Double.toString(lon) +
* "},\"accuracy\":50.0,\"name\":\"event\",\"types\":[\"school\"],\"language\":\"en\"}"
* );
*/
reqUrl.put(
"Host: maps.googleapis.com",
"{\"location\":{\"lat\":1.347988,\"lng\":103.936179},\"accuracy\":50.0,\"name\":\"events\",\"types\":[\"school\"],\"language\":\"en\"}");
Log.v(LOG_KEY, "Requested URL= " + reqUrl);
HttpRequestFactory httpRequestFactory = createRequestFactory(HTTP_TRANSPORT);
HttpRequest request = httpRequestFactory
.buildGetRequest(reqUrl);
Log.v(LOG_KEY, request.execute().parseAsString());
place = request.execute().parseAs(PlacesList.class);
Log.v(LOG_KEY, "STATUS = " + place.status);
Log.v(LOG_KEY, "Place Added is = " + place);
return place;
} catch (HttpResponseException e) {
// Log.v(LOG_KEY, e.getResponse().parseAsString());
Log.v(LOG_KEY, e.getMessage());
try {
throw e;
} catch (HttpResponseException e1) {
e1.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return place;
}
}
已编辑
public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context context;
public AddItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public AddItemizedOverlay(Drawable defaultMarker, Context context) {
this(defaultMarker);
this.context = context;
}
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
if (event.getAction() == 1) {
GeoPoint geopoint = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
// latitude
double lat = geopoint.getLatitudeE6() / 1E6;
// longitude
double lon = geopoint.getLongitudeE6() / 1E6;
//Toast.makeText(context, "Lat: " + lat + ", Lon: "+lon, Toast.LENGTH_SHORT).show();
}
return false;
}
@Override
protected OverlayItem createItem(int i) {
return mapOverlays.get(i);
}
@Override
public int size() {
return mapOverlays.size();
}
@Override
protected boolean onTap(int index) {
OverlayItem item = mapOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(this.context);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.show();
return true;
}
public void addOverlay(OverlayItem overlay) {
mapOverlays.add(overlay);
}
public void populateNow(){
this.populate();
}
}
覆盖
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_places);
// Getting intent data
Intent i = getIntent();
// Users current geo location
String user_latitude = i.getStringExtra("user_latitude");
String user_longitude = i.getStringExtra("user_longitude");
// Nearplaces list
nearPlaces = (PlacesList) i.getSerializableExtra("near_places");
mapView = (MapView) findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
mapOverlays = mapView.getOverlays();
// Geopoint to place on map
geoPoint = new GeoPoint((int) (Double.parseDouble(user_latitude) * 1E6),
(int) (Double.parseDouble(user_longitude) * 1E6));
// Drawable marker icon
Drawable drawable_user = this.getResources()
.getDrawable(R.drawable.mark_red);
itemizedOverlay = new AddItemizedOverlay(drawable_user, this);
// Map overlay item
overlayitem = new OverlayItem(geoPoint, "Your Location",
"That is you!");
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
itemizedOverlay.populateNow();
// Drawable marker icon
Drawable drawable = this.getResources()
.getDrawable(R.drawable.mark_blue);
itemizedOverlay = new AddItemizedOverlay(drawable, this);
mc = mapView.getController();
// These values are used to get map boundary area
// The area where you can see all the markers on screen
int minLat = Integer.MAX_VALUE;
int minLong = Integer.MAX_VALUE;
int maxLat = Integer.MIN_VALUE;
int maxLong = Integer.MIN_VALUE;
// check for null in case it is null
if (nearPlaces.results != null) {
// loop through all the places
for (Place place : nearPlaces.results) {
latitude = place.geometry.location.lat; // latitude
longitude = place.geometry.location.lng; // longitude
// Geopoint to place on map
geoPoint = new GeoPoint((int) (latitude * 1E6),
(int) (longitude * 1E6));
// Map overlay item
overlayitem = new OverlayItem(geoPoint, place.name,
place.vicinity);
itemizedOverlay.addOverlay(overlayitem);
// calculating map boundary area
minLat = (int) Math.min( geoPoint.getLatitudeE6(), minLat );
minLong = (int) Math.min( geoPoint.getLongitudeE6(), minLong);
maxLat = (int) Math.max( geoPoint.getLatitudeE6(), maxLat );
maxLong = (int) Math.max( geoPoint.getLongitudeE6(), maxLong );
}
mapOverlays.add(itemizedOverlay);
// showing all overlay items
itemizedOverlay.populateNow();
}
// Adjusting the zoom level so that you can see all the markers on map
mapView.getController().zoomToSpan(Math.abs( minLat - maxLat ), Math.abs( minLong - maxLong ));
// Showing the center of the map
mc.animateTo(new GeoPoint((maxLat + minLat)/2, (maxLong + minLong)/2 ));
mapView.postInvalidate();
}
@Override
protected boolean isRouteDisplayed() {
return false;
}