标记点击意图不起作用

时间:2014-01-23 12:04:53

标签: android android-intent directions driving-directions android-maps-extensions

我已经实现了OnMarkerClickListener,并修改了Is there an API for Google Maps navigation in Android?中的代码以查看它是否可行,但是当您点击标记时,地图会放大它?我错过了什么? - 编辑:我已经在下面编辑了代码,现在当你点击标记时应用程序崩溃了,不幸的是你的应用程序停止运行

由于

@Override //This is going after my setUpMap(); method
public boolean onMarkerClick(Marker marker) {
double lati = marker.getPosition().latitude;
double longi = marker.getPosition().longitude;
Location currentLocation = mMap.getMyLocation();
double latitudeCurr = currentLocation.getLatitude();
double longitudeCurr = currentLocation.getLongitude();
final Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("+http://maps.google.com/maps?" + "saddr=" + latitudeCurr + "," + longitudeCurr + "&daddr=" + lati + "," + longi));
    intent.setClassName("com.google.android.apps.maps","com.google.android.maps.MapsActivity");
startActivity(intent);
return true;
}

这是LogCat文件

01-24 10:32:59.148: W/dalvikvm(27979): threadid=1: thread exiting with uncaught exception (group=0x418fb898)
01-24 10:32:59.168: E/AndroidRuntime(27979): FATAL EXCEPTION: main
01-24 10:32:59.168: E/AndroidRuntime(27979): java.lang.NullPointerException
01-24 10:32:59.168: E/AndroidRuntime(27979):    at com.mypackage.mypackagename.MainActivity.onMarkerClick(MainActivity.java:186)
01-24 10:32:59.168: E/AndroidRuntime(27979):    at com.androidmapsextensions.impl.DelegatingGoogleMap$DelegatingOnMarkerClickListener.onMarkerClick(DelegatingGoogleMap.java:444)
01-24 10:32:59.168: E/AndroidRuntime(27979):    at com.google.android.gms.maps.GoogleMap$8.a(Unknown Source)
01-24 10:32:59.168: E/AndroidRuntime(27979):    at com.google.android.gms.maps.internal.k$a.onTransact(Unknown Source)
01-24 10:32:59.168: E/AndroidRuntime(27979):    at android.os.Binder.transact(Binder.java:347)
01-24 10:32:59.168: E/AndroidRuntime(27979):    at ecs.a(SourceFile:84)
01-24 10:32:59.168: E/AndroidRuntime(27979):    at maps.af.bl.a(Unknown Source)
01-24 10:32:59.168: E/AndroidRuntime(27979):    at maps.ao.c.a(Unknown Source)
01-24 10:32:59.168: E/AndroidRuntime(27979):    at maps.ao.n.c(Unknown Source)
01-24 10:32:59.168: E/AndroidRuntime(27979):    at maps.ao.m.a(Unknown Source)
01-24 10:32:59.168: E/AndroidRuntime(27979):    at maps.ao.an.b(Unknown Source)
01-24 10:32:59.168: E/AndroidRuntime(27979):    at maps.ao.bc.onSingleTapConfirmed(Unknown Source)
01-24 10:32:59.168: E/AndroidRuntime(27979):    at maps.br.g.onSingleTapConfirmed(Unknown Source)
01-24 10:32:59.168: E/AndroidRuntime(27979):    at maps.br.i.handleMessage(Unknown Source)
01-24 10:32:59.168: E/AndroidRuntime(27979):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-24 10:32:59.168: E/AndroidRuntime(27979):    at android.os.Looper.loop(Looper.java:137)
01-24 10:32:59.168: E/AndroidRuntime(27979):    at android.app.ActivityThread.main(ActivityThread.java:5419)
01-24 10:32:59.168: E/AndroidRuntime(27979):    at java.lang.reflect.Method.invokeNative(Native Method)
01-24 10:32:59.168: E/AndroidRuntime(27979):    at java.lang.reflect.Method.invoke(Method.java:525)
01-24 10:32:59.168: E/AndroidRuntime(27979):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
01-24 10:32:59.168: E/AndroidRuntime(27979):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
01-24 10:32:59.168: E/AndroidRuntime(27979):    at dalvik.system.NativeStart.main(Native Method)

这是完整的MainActivity

import java.io.InputStream;
import java.util.ArrayList;

import com.androidmapsextensions.ClusteringSettings;
import com.androidmapsextensions.GoogleMap.OnMarkerClickListener;
import com.androidmapsextensions.Marker;
import com.androidmapsextensions.MarkerOptions;
import com.androidmapsextensions.SupportMapFragment;
import com.androidmapsextensions.GoogleMap;

import android.support.v4.app.FragmentActivity;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.LocationSource;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;

public class MainActivity extends FragmentActivity implements OnMarkerClickListener, LocationListener,
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener, LocationSource {

private OnLocationChangedListener mListener;
private LocationManager locationmanager;
GoogleMap mMap;
LatLng myposition;
ArrayList<Entry> entries = new ArrayList<Entry>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationmanager = (LocationManager) getSystemService(LOCATION_SERVICE);

if(locationmanager != null){
boolean gpsIsEnabled = locationmanager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean networkIsEnabled = locationmanager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

if(gpsIsEnabled){
locationmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000L, 10F, this);
}
else if(networkIsEnabled)
{
locationmanager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000L, 10F, this);
}
else
{
Toast.makeText(this, "GPS is not enabled", Toast.LENGTH_SHORT).show();
}

setUpMapIfNeeded();
showmypoi();
}

}

@Override
public void onPause()
{
if(locationmanager != null){
locationmanager.removeUpdates(this);
}
super.onPause();

}

@Override
public void onResume(){
super.onResume();
setUpMapIfNeeded();
if(locationmanager != null){
mMap.setMyLocationEnabled(true);
}
}

private void setUpMapIfNeeded() {
if (mMap == null){
mMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.the_map)).getExtendedMap();

if (mMap != null){
setUpMap();
}
mMap.setLocationSource(this);
mMap.setMyLocationEnabled(true);
}

}

private void setUpMap() {
mMap.setMyLocationEnabled(true);
mMap.setOnMarkerClickListener(this);
}

private void showmypoi() {
InputStream is = null;
is = this.getResources().openRawResource(R.raw.markers);

ArrayList<Entry> entries = (ArrayList<Entry>) new ParserParsing().parse(is);
for (int i = 0; i < entries.size(); i++){
Double latitude = Double.valueOf(entries.get(i).lat);
Double longitude = Double.valueOf(entries.get(i).lng);
String icon = entries.get(i).icon;
LatLng lng = new LatLng(latitude,longitude);
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.the_map)).getExtendedMap();
mMap.setClustering(new ClusteringSettings().enabled(true).addMarkersDynamically(true));
mMap.addMarker(new MarkerOptions()
.position(lng)
.icon(BitmapDescriptorFactory.fromResource(getResources().getIdentifier(icon, "drawable", getPackageName()))));
} 
}

@Override
public void activate(OnLocationChangedListener listener) {
mListener = listener;
}

@Override
public void deactivate() {
mListener = null;
}

@Override
public void onConnectionFailed(ConnectionResult result) {}

@Override
public void onConnected(Bundle connectionHint) {}

@Override
public void onDisconnected() {}

public void onCameraChange(CameraPosition cameraPosition){

}

@Override
public void onLocationChanged(Location location) {
if( mListener != null){
mListener.onLocationChanged( location );

LatLngBounds bounds = this.mMap.getProjection().getVisibleRegion().latLngBounds;

if(!bounds.contains(new LatLng(location.getAltitude(), location.getLongitude())));{
mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude())));
}

}

}

@Override
public void onProviderDisabled(String provider) {
Toast.makeText(this, "No Network Data Available", Toast.LENGTH_SHORT).show();

}

@Override
public void onProviderEnabled(String provider) {
Toast.makeText(this, "Using Network Data", Toast.LENGTH_SHORT).show();

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Toast.makeText(this, "Status Changed", Toast.LENGTH_SHORT).show();

}

@Override
public boolean onMarkerClick(Marker marker) {
Location currentLocation = locationmanager.getLastKnownLocation(LOCATION_SERVICE);//mMap.getMyLocation();
double latitude = marker.getPosition().latitude;
double longitude = marker.getPosition().longitude;
double latitudeCurr = currentLocation.getLatitude();
double longitudeCurr = currentLocation.getLongitude();
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("https://maps.google.co.uk/maps?" + "saddr="
+ latitudeCurr + "," + longitudeCurr + "&daddr="
+ latitude + "," + longitude));
intent.setClassName("com.google.android.apps.maps",
"com.google.android.maps.MapsActivity");
startActivity(intent);
return false;
}



}

权限:

<permission android:name="com.mypackage.mypackagename.permission.MAPS_RECEIVE" android:protectionLevel="signature" />
<uses-permission android:name="com.mypackage.mypackagename.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

1 个答案:

答案 0 :(得分:1)

我已经弄清楚了。而不是使用

@Override //This is going after my setUpMap(); method
public boolean onMarkerClick(Marker marker) {
double lati = marker.getPosition().latitude;
double longi = marker.getPosition().longitude;
Location currentLocation = mMap.getMyLocation();
double latitudeCurr = currentLocation.getLatitude();
double longitudeCurr = currentLocation.getLongitude();
final Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("+http://maps.google.com/maps?" + "saddr=" + latitudeCurr + "," + longitudeCurr + "&daddr=" + lati + "," + longi));
    intent.setClassName("com.google.android.apps.maps","com.google.android.maps.MapsActivity");
startActivity(intent);
return true;
}

我将当前的位置代码行更改为

@Override //This is going after my setUpMap(); method
public boolean onMarkerClick(Marker marker) {
double lati = marker.getPosition().latitude;
double longi = marker.getPosition().longitude;
double latitudeCurr = mMap.getMyLocation().getLatitude();
double longitudeCurr = mMap.getMyLocation().getLongitude();
final Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("+http://maps.google.com/maps?" + "saddr=" + latitudeCurr + "," + longitudeCurr + "&daddr=" + lati + "," + longi));
    intent.setClassName("com.google.android.apps.maps","com.google.android.maps.MapsActivity");
startActivity(intent);
return true;
}

现在可以使用,并打开Goog​​le地图应用