当前位置: 首页 > 知识库问答 >
问题:

标记点击意图不起作用

方浩旷
2023-03-14
@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;
}
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)
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个答案

孟跃
2023-03-14

我已经想通了。而不是使用

@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;
}

现在可以工作了,打开谷歌地图应用程序

 类似资料:
  • 嗨,我有问题,如何实现点击标记的信息窗口?问题是我在地图上有很多标记,如果单击信息窗口,每个标记都有另一个活动<代码>在此处输入代码 以下是示例 标记1----- 我尝试了很多代码但没有解决

  • 这是我的密码。这很简单,但当我点击按钮“button1”时,什么都没有发生。我做错了什么? 第二个-- 第三个-- 布局-- 新Logcat:11-29 12:15:39.552:D/gralloc_goldfish(613):检测到没有GPU仿真的模拟器。11-29 12:15:40.062:I/编舞(613):跳过51帧!应用程序可能在其主线程上做了太多工作。11-29 12:16:08.90

  • 当我点击地图上的某个地方,应用程序绘制当前位置和目的地之间的折线(点击)。我希望目标标记是点击自己所有的时间(每几秒钟),所以在用户移动新的折线是绘制(更新最后一个)。

  • 我还是Selenium测试世界的新手,目前正在使用SeleniumChromeWebDriver进行测试。 我想做的是在下面的弹出对话框中: < li >在文本输入框中输入一个数字 < li >单击应用按钮 < li >然后将触发web应用程序向服务器端发布信息。 这是我到目前为止发现的,如果我取消对<code>线程的注释。睡眠(500),一切都将完美工作。但是没有<code>线程。sleep(5

  • 我试图在facebook messenger上共享音频文件。如下所述https://developers.facebook.com/docs/messenger/android#integration_with_intents在这里,它可以共享简单的文本而不是音频文件。当我尝试在messenger上发送音频时,应用程序崩溃。 这是意图共享代码字符串mimeType=“音频/aac”;