我按照
中的教程进行操作https://developers.google.com/maps/documentation/android/start
我设法将地图加载到我的应用程序中。现在我想使用纬度和经度将一些标记添加到我的应用程序中。我该怎么办?我尝试了一些东西,但没有奏效。我试过的代码如下。
这是我怎么做的。但我得到一个零点异常。这是什么原因?
GoogleMap googleMap = null;
MapFragment fm = (MapFragment) (activity.getFragmentManager())
.findFragmentById(R.id.map);
googleMap = fm.getMap();
double latitude = 7.421226;
double longitude =80.401264 ;
// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps ");
// adding marker
googleMap.addMarker(marker);
我的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/filmhall"
android:background="@color/grey" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>
答案 0 :(得分:5)
看看使用这个方法我们在谷歌地图上添加标记
mMap.addMarker(new MarkerOptions().position(new LatLng(22.7253, 75.8655)).title("Indore"));
这是示例
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class BasicMapDemoActivity extends FragmentActivity
{
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.basic_demo);
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded()
{
if (mMap == null)
{
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
if (mMap != null)
{
setUpMap();
}
}
}
private void setUpMap()
{
mMap.addMarker(new MarkerOptions().position(new LatLng(22.7253, 75.8655)).title("Indore"));
// here is marker Adding code
}
}
答案 1 :(得分:1)
// latitude and longitude
double latitude = ;
double longitude = ;
// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps ");
// adding marker
googleMap.addMarker(marker);
答案 2 :(得分:1)
这里是我的代码(它在地图中有4个图标,你只需将这个类称为主要活动中的片段,它将起作用,记住:在drawable中创建一个图片并将其命名为& #34; atmpin2.png&#34):
package fragments;
import com.einstein.atm.R;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.DisplayMetrics;
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.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Marker;
public class FragMapsGoogle extends MapFragment {
GoogleMap map;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = super.onCreateView(inflater, container,
savedInstanceState);
map = getMap();
LatLng place = new LatLng(21, 105.8);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(place, 14.0f));
DisplayMetrics displaymetrics = new DisplayMetrics();
int w,h;
this.getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
h = displaymetrics.heightPixels;
w = displaymetrics.widthPixels;
Bitmap bmp = BitmapFactory.decodeResource(
getResources(), R.drawable.atmpin2);
float h2w2 = (float)bmp.getHeight()/bmp.getWidth();
GoogleMap map = this.getMap();
Toast.makeText(this.getActivity(), "Size Screen: "+h+"X"+w+" "+h2w2, Toast.LENGTH_LONG).show();
for (int i = 1; i < 5; i++) {
MarkerOptions mkop = new MarkerOptions();
place = new LatLng(21+i*0.00055, 105.8+i*0.00054);
mkop.position(place);
Marker maker = map.addMarker(mkop);
maker.setIcon(BitmapDescriptorFactory.fromBitmap(Bitmap.createScaledBitmap(bmp, w/20+5, (int)((w/20+5)*h2w2), true)));
}
return rootView;
}
}
// Ha Noi: 21.0226967,105.8369637
答案 3 :(得分:0)
static final LatLng PERTH = new LatLng(-31.90, 115.86);
Marker perth = mMap.addMarker(new MarkerOptions()
.position(PERTH)
.draggable(true));
检查此link