我正在开展一个项目,我必须为当前位置添加标记,并根据我的json响应添加多个标记。
一切正常但在将setInfoAdapter
添加到地图对象后,当我点按该标记时,显示我当前位置的标记会导致应用崩溃。
我哪里错了?请建议我。
这是我的活动。
public class MapActivity extends Activity implements LocationListener {
private ProgressDialog pDialog;
private GoogleMap gMap;
private LocationManager locationManager;
private String provider;
boolean gps_enabled = false;
boolean netwrk_enabled = false;
String phone = null;
JSONArray json;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
gMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
23.40276491, 77.51953125), 5));
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
gps_enabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
netwrk_enabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (gps_enabled)
provider = LocationManager.GPS_PROVIDER;
else
provider = LocationManager.NETWORK_PROVIDER;
locationManager.requestLocationUpdates(provider, 5000L, 10F, this);
Location location = locationManager.getLastKnownLocation(provider);
//Moving and adding marker to my current location here
CameraPosition cp = new CameraPosition(new LatLng(
location.getLatitude(), location.getLongitude()), 13, 0, 0);
gMap.animateCamera(CameraUpdateFactory.newCameraPosition(cp), 3000,
null);
gMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
Marker marker = gMap.addMarker(new MarkerOptions()
.position(
new LatLng(location.getLatitude(), location
.getLongitude())).title("I am here").icon(BitmapDescriptorFactory.fromResource(R.drawable.mrk1)));
marker.showInfoWindow();
if (location != null) {
if (provider == LocationManager.GPS_PROVIDER)
Toast.makeText(getApplicationContext(), "location by gps",
Toast.LENGTH_SHORT).show();
else if (provider == LocationManager.NETWORK_PROVIDER)
Toast.makeText(getApplicationContext(), "location by network",
Toast.LENGTH_SHORT).show();
new HttpGetTask().execute();
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub18d1:d002
super.onPause();
locationManager.removeUpdates(this);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.map, menu);
return true;
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(this, "Disabled provider " + provider,
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
private class HttpGetTask extends AsyncTask<Void, Void, String> {
//http request
}
@Override
protected void onPostExecute(String result) {
if (pDialog.isShowing())
pDialog.dismiss();
try {
json = new JSONArray(result);
for (int i = 0; i < json.length(); i++) {
Log.v("Response", result);
final JSONObject e = json.getJSONObject(i);
String point = e.getString("point");
Log.v("POINT", point);
phone = e.getString("ph");
Log.v("PH", phone);
String[] point2 = point.split(",");
double lat1 = Double.parseDouble(point2[0]);
double lng1 = Double.parseDouble(point2[1]);
Log.v("LLDN", "" + lat1 + "&" + lng1);
//Adding multiple markers to my map
gMap.addMarker(new MarkerOptions()
.title(e.getString("name"))
.snippet(
e.getString("LS")+"-"+e.getString("ph") )
.position(new LatLng(lng1, lat1))
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.pmr)));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Adding infoWindowAdapter to my markers.
gMap.setInfoWindowAdapter(new InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public View getInfoContents(Marker mrkr) {
// TODO Auto-generated method stub
String name = mrkr.getTitle();
String detail = mrkr.getSnippet();
String trimmedDetail = detail.substring(0, 60);
Log.v("Info", name + " " + detail);
View v = getLayoutInflater().inflate(R.layout.infowindow,
null);
TextView title = (TextView) v.findViewById(R.id.titleTV);
TextView snippet = (TextView) v
.findViewById(R.id.snippetTV);
title.setText("" + name);
snippet.setText("" + trimmedDetail);
return v;
}
});
gMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker arg0) {
Intent myIntent = new Intent(getBaseContext(),
DtActivity.class);
myIntent.putExtra("title", arg0.getTitle());
myIntent.putExtra("detail", arg0.getSnippet());
//myIntent.putExtra("ph1", phone);
startActivity(myIntent);
}
});
if (null != mClient)
mClient.close();
}
}
导致NullPointerException
,因为我没有在该标记中传递代码段。
我不想要任何infoWindow作为该标记。如何实现这一目标。?
我尝试再创建一个GoogleMap
对象,并添加了显示该对象当前位置的标记,但结果相同。
我尝试禁用该标记的MouseClickEvent
。它禁用所有标记点击事件。
我可以使用叠加添加该标记。?
答案 0 :(得分:0)
我编写了一种在Google地图上添加标记的方法,通过它我可以找到标记ID。看看下面:
String markerID = null;
private void addMarker(GoogleMap map, double lat, double lon, String title, String snippet, int resourceId)
{
markerID = map.addMarker(new MarkerOptions().position(new LatLng(lat, lon)).title(title).snippet(snippet).icon(BitmapDescriptorFactory.fromResource(resourceId))).getId();
}
并使用该标记ID在标记的OnClickListener
中进行匹配:
@Override
public View getInfoWindow(final Marker marker)
{
if(!marker.getId().equals(markerID))
//do your code
else
//do your code
}