我从教程Android Working with Google Places and Maps中获得了这段代码,我想更改它,因为我需要它与 google maps v2 一起使用。问题是,我只在谷歌地点后面得到一个网格,而不是地图。
PlacesMapActivity.java
package com.androidhive.googleplacesandmaps;
import java.util.List;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class PlacesMapActivity extends MapActivity {
// Nearest places
PlacesList nearPlaces;
// Map view
MapView mapView;
// Map overlay items
List<Overlay> mapOverlays;
AddItemizedOverlay itemizedOverlay;
GeoPoint geoPoint;
// Map controllers
MapController mc;
double latitude;
double longitude;
OverlayItem overlayitem;
@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;
}
}
GooglePlaces.java
package com.androidhive.googleplacesandmaps;
import org.apache.http.client.HttpResponseException;
import android.content.Context;
import android.util.Log;
import com.google.api.client.googleapis.GoogleHeaders;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.http.json.JsonHttpParser;
import com.google.api.client.json.jackson.JacksonFactory;
@SuppressWarnings("deprecation")
public class GooglePlaces {
/** Global instance of the HTTP transport. */
private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
// Google API Key
private static final String API_KEY = "****my key is here*****"; // my API key Key for browser apps
// Google Places search url's
private static final String PLACES_SEARCH_URL = "https://maps.googleapis.com/maps/api/place/search/json?";
private static final String PLACES_TEXT_SEARCH_URL = "https://maps.googleapis.com/maps/api/place/search/json?";
private static final String PLACES_DETAILS_URL = "https://maps.googleapis.com/maps/api/place/details/json?";
private double _latitude;
private double _longitude;
private double _radius;
/**
* Searching places
* @param latitude - latitude of place
* @params longitude - longitude of place
* @param radius - radius of searchable area
* @param types - type of place to search
* @return list of places
* */
public PlacesList search(double latitude, double longitude, double radius, String types)
throws Exception {
this._latitude = latitude;
this._longitude = longitude;
this._radius = 3000;
try {
HttpRequestFactory httpRequestFactory = createRequestFactory(HTTP_TRANSPORT);
HttpRequest request = httpRequestFactory
.buildGetRequest(new GenericUrl(PLACES_SEARCH_URL));
request.getUrl().put("key", API_KEY);
request.getUrl().put("location", _latitude + "," + _longitude);
request.getUrl().put("radius", _radius); // in meters
request.getUrl().put("sensor", "false");
if(types != null)
request.getUrl().put("types", types);
PlacesList list = request.execute().parseAs(PlacesList.class);
// Check log cat for places response status
Log.d("Places Status", "" + list.status);
return list;
} catch (HttpResponseException e) {
Log.e("Error:", e.getMessage());
return null;
}
}
/**
* Searching single place full details
* @param refrence - reference id of place
* - which you will get in search api request
* */
public PlaceDetails getPlaceDetails(String reference) throws Exception {
try {
HttpRequestFactory httpRequestFactory = createRequestFactory(HTTP_TRANSPORT);
HttpRequest request = httpRequestFactory
.buildGetRequest(new GenericUrl(PLACES_DETAILS_URL));
request.getUrl().put("key", API_KEY);
request.getUrl().put("reference", reference);
request.getUrl().put("sensor", "false");
PlaceDetails place = request.execute().parseAs(PlaceDetails.class);
return place;
} catch (HttpResponseException e) {
Log.e("Error in Perform Details", e.getMessage());
throw e;
}
}
/**
* Creating http request Factory
* */
public static HttpRequestFactory createRequestFactory(
final HttpTransport transport) {
return transport.createRequestFactory(new HttpRequestInitializer() {
public void initialize(HttpRequest request) {
GoogleHeaders headers = new GoogleHeaders();
headers.setApplicationName("AndroidHive-Places-Test");
request.setHeaders(headers);
JsonHttpParser parser = new JsonHttpParser(new JacksonFactory());
request.addParser(parser);
}
});
}
}
我正在尝试很多事情,但无法让它发挥作用,即使我设法让谷歌地图显示在我的设备中的另一个项目中。我到底要改变什么?
我的日志猫
02-10 16:42:42.479: W/KeyCharacterMap(11974): Can't open keycharmap file
02-10 16:42:42.479: W/KeyCharacterMap(11974): Error loading keycharmap file '/system/usr/keychars/ft5x02-touchscreen.kcm.bin'. hw.keyboards.65538.devname='ft5x02-touchscreen'
02-10 16:42:42.479: W/KeyCharacterMap(11974): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
02-10 16:42:54.067: D/Network(11974): Network Enabled
02-10 16:42:54.077: D/Your Location(11974): latitude:37.970373475, longitude: 23.772967
02-10 16:42:54.507: D/Places Status(11974): OK
02-10 16:42:57.917: D/Place(11974): Agora Ilission S.A.Chatzigianni Mexi 8, Athens, Greece21 0725 225237.97706123.751386
02-10 16:43:00.407: D/Place(11974): Piu VerdeΣτρατ. Αλ. Παπάγου, Athens, Greece21 0654 618537.99337223.794133
02-10 16:43:01.127: W/KeyCharacterMap(11974): Can't open keycharmap file
02-10 16:43:01.127: W/KeyCharacterMap(11974): Error loading keycharmap file '/system/usr/keychars/ft5x02-touchscreen.kcm.bin'. hw.keyboards.65538.devname='ft5x02-touchscreen'
02-10 16:43:01.127: W/KeyCharacterMap(11974): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
02-10 16:43:02.497: W/MapActivity(11974): Recycling dispatcher com.google.googlenav.datarequest.DataRequestDispatcher@45202618
02-10 16:43:02.507: V/MapActivity(11974): Recycling map object.
02-10 16:43:02.617: I/MapActivity(11974): Handling network change notification:CONNECTED
02-10 16:43:02.617: E/MapActivity(11974): Couldn't get connection factory client