我正在尝试实现一个简单的应用程序,并使用我试图在地图中标记斑点的片段。
我的片段包含以下代码:
package pt.com.hugo_dias.hstapplication.customer.fragments;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import java.util.List;
import pt.com.hugo_dias.HSTApplication;
import pt.com.hugo_dias.hstapplication.R;
import pt.com.hugo_dias.hstapplication.customer.database.Customer;
import pt.com.hugo_dias.hstapplication.customer.database.CustomerDatabaseHelper;
import pt.com.hugo_dias.hstapplication.customer.exceptions.CustomerException;
public class GoogleMapFragment extends SupportMapFragment implements OnMapReadyCallback{
/**
* The fragment argument representing the item ID that this fragment
* represents.
*/
private CustomerDatabaseHelper customerHelper;
private GoogleMap mMap;
public GoogleMapFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
getActivity().invalidateOptionsMenu();
View rootView = null;
long id = getActivity().getIntent().getLongExtra("ID", 0);
rootView = inflater.inflate(R.layout.fragment_map, container, false);
getMapAsync(this);
return rootView;
}
public CustomerDatabaseHelper getCustomerHelper() {
if (customerHelper == null) {
customerHelper = new CustomerDatabaseHelper(getActivity());
}
return customerHelper;
}
public void setCustomerHelper(CustomerDatabaseHelper customerHelper) {
this.customerHelper = customerHelper;
}
@Override
public void onDestroyView() {
super.onDestroyView();
mMap = null;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
long id = getActivity().getIntent().getLongExtra("ID", 0);
if (id == 0) {
Log.i(HSTApplication.TAG, "Customer id cannot be 0 ");
Toast.makeText(getActivity(), R.string.customerdoesntexists,
Toast.LENGTH_LONG).show();
return;
}
Customer customer;
try {
customer = getCustomerHelper().getCustomer(id);
} catch (CustomerException e) {
Log.i(HSTApplication.TAG, "Customer does not exists - " + id);
Toast.makeText(getActivity(), R.string.customerdoesntexists,
Toast.LENGTH_LONG).show();
return;
}
double latitude = customer.getLatitude();
double longitude = customer.getLongitude();
LatLng position = new LatLng(latitude, longitude);
googleMap.setMyLocationEnabled(true);
googleMap.addMarker(new MarkerOptions().position(
position).title(customer.getName()).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 10));
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
try {
List<Customer> dependencies = getCustomerHelper().getDependencies(id);
for(Customer dependency : dependencies){
position = new LatLng(dependency.getLatitude(), dependency.getLongitude());
googleMap.addMarker(new MarkerOptions().position(
position).title(dependency.getName()).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
}
} catch (CustomerException e) {
e.printStackTrace();
}
}
}
我不明白为什么没有显示标记,但我看到在调试期间调用的方法。
答案 0 :(得分:0)
您的代码给人的印象是,在创建CustomerDatabaseHelper
对象时,您正在从数据库中提取标记。但最终缺少该文件的代码。如果您只想长时间点击地图片段来显示标记而不将其存储到数据库中并将其取出使事情变得简单,只需将此代码复制并粘贴到MainActivity.java中:
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.MapFragment;
//import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.location.Location;
import android.os.Bundle;
//import android.support.v4.app.FragmentActivity;
//import android.support.v4.app.FragmentManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity
implements OnMapClickListener, OnMapLongClickListener{
final int RQS_GooglePlayServices = 1;
private GoogleMap myMap;
Location myLocation;
TextView tvLocInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvLocInfo = (TextView)findViewById(R.id.locinfo);
FragmentManager myFragmentManager = getFragmentManager();
MapFragment myMapFragment
= (MapFragment)myFragmentManager.findFragmentById(R.id.map);
myMap = myMapFragment.getMap();
myMap.setMyLocationEnabled(true);
myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
//myMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//myMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
//myMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
myMap.setOnMapClickListener(this);
myMap.setOnMapLongClickListener(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.activity_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_legalnotices:
String LicenseInfo = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(
getApplicationContext());
AlertDialog.Builder LicenseDialog = new AlertDialog.Builder(MainActivity.this);
LicenseDialog.setTitle("Legal Notices");
LicenseDialog.setMessage(LicenseInfo);
LicenseDialog.show();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
if (resultCode == ConnectionResult.SUCCESS){
Toast.makeText(getApplicationContext(),
"isGooglePlayServicesAvailable SUCCESS",
Toast.LENGTH_LONG).show();
}else{
GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices);
}
}
@Override
public void onMapClick(LatLng point) {
tvLocInfo.setText(point.toString());
myMap.animateCamera(CameraUpdateFactory.newLatLng(point));
}
@Override
public void onMapLongClick(LatLng point) {
tvLocInfo.setText("New marker added@" + point.toString());
myMap.addMarker(new MarkerOptions().position(point).title(point.toString()));
}
}
希望有助于!!