Olá, tudo bem?
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import android.os.Bundle;
public class Localizador extends MapActivity{
MapView mapView = null;
MapController mapController = null;
MyLocationOverlay whereAmi = null;
@Override
protected boolean isLocationDisplayed(){
return whereAmi.isMyLocationEnabled();
}
@Override
protected boolean isRouteDisplayed(){
return false;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.localizadordeacougues);
mapView = (MapView) findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
mapController = mapView.getController();
mapController.setZoom(17);
whereAmi = new MyCustomLocationOverlay(this, mapView);
mapView.getOverlays().add(whereAmi);
mapView.postInvalidate();
}
@Override
public void onResume(){
super.onResume();
whereAmi.enableMyLocation();
whereAmi.runOnFirstFix(new Runnable() {
public void run() {
mapController.setCenter(whereAmi.getMyLocation());
}
});
}
@Override
public void onPause(){
super.onPause();
whereAmi.disableMyLocation();
}
}
Ae eu customizei minha classe LocationOverlay para mostrar ao usuario se ele distanciar o mapa do local onde ele está o mapa voltar para o centro
MyCustomOverlay.class
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import android.content.Context;
import android.location.Location;
public class MyCustomLocationOverlay extends MyLocationOverlay {
MapView mMapView = null;
public MyCustomLocationOverlay(Context ctx, MapView mapView) {
super(ctx, mapView);
mMapView = mapView;
}
public void onLocationChanged(Location loc){
super.onLocationChanged(loc);
GeoPoint newPt = new GeoPoint((int) (loc.getLatitude()*1E6), (int)(loc.getLongitude()*1E6));
mMapView.getController().animateTo(newPt);
StoresNearMe store = new StoresNearMe(loc);
store.getClass();
}
}
Eu também tenho uma classe que se comunica com a api do google maps na qual não consigo utilizar, é nisso que preciso de uma mão
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.location.Location;
import android.util.Log;
public class StoresNearMe {
private String result = "";
private String googleAPIKey = "Oculto Para Essa Mensagem";
private String searchRadius = "1000";
private String types = "types";
private Location myLocation;
final String TAG = getClass().getSimpleName();
public StoresNearMe(Location location){
myLocation = location;
}//StoresNearMe
public String getStoresNearMe(){
String result = "Nothing";
result = callGoogleWebService(buildURLForGooglePlaces(myLocation));
convertJSONtoArray(result);
return result;
}//getStoresNearMe
private String callGoogleWebService(String url){
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
//request.addHeader("deviceId", deviceId);
ResponseHandler<String> handler = new BasicResponseHandler();
try {
result = httpclient.execute(request, handler);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
httpclient.getConnectionManager().shutdown();
return result;
}//callWebService
private String buildURLForGooglePlaces(Location location){
String baseUrl = "https://maps.googleapis.com/maps/api/place/search/json?";
String lat = String.valueOf(location.getLatitude());
String lon = String.valueOf(location.getLongitude());
String url = baseUrl + "location=" + lat + "," + lon + "&" +
"radius=" + searchRadius + "&" + types + "&" + "sensor=true" +
"&" + "key=" + googleAPIKey;
Log.d(TAG,url);
return url;
}//buildURLForGooglePlaces
private void convertJSONtoArray(String rawJSON){
try {
JSONObject completeJSONObj = new JSONObject(rawJSON);
String json = completeJSONObj.toString();
Log.d(TAG,json);
JSONArray results = completeJSONObj.getJSONArray("results");
} catch (JSONException e) {
Log.d(TAG,"JSON parsing error - fix it:" + e.getMessage());
}
}//convertJSONtoArray
}//StoresNearMe
Ja faz um tempo que estou procurando se é isso mesmo, se preciso fazer isso, se o Maps Api tem alguma coisa parecida, etc...
0 comentários:
Postar um comentário