我遵循Google Developers
网站上给出的所有内容,并将此代码输入MapsActivity.java
package com.pro.soft.inzi.track_trace;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
Button sign_in_btn = (Button) findViewById(R.id.b1);
sign_in_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),Live.class);
startActivity(intent);
}
});
Button history_btn = (Button) findViewById(R.id.b2);
history_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), History.class);
startActivity(intent);
}
});
}
@Override
public void onMapReady(GoogleMap googleMap) {
// Add a marker in Pakistan and move the camera
LatLng Pakistan = new LatLng(31, 71);
googleMap.addMarker(new MarkerOptions().position(Pakistan).title("Marker in Pakistan"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(Pakistan));
}
}
这些是
AndroidManifest.xml
文件中的权限<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" />
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but are recommended.
-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!--
To retrieve OAuth 2.0 tokens or invalidate tokens to disconnect a user. This disconnect
option is required to comply with the Google+ Sign-In developer policies
-->
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<!-- To retrieve the account name (email) as part of sign-in: -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<permission
android:name="your.package.name.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="your.package.name.permission.MAPS_RECEIVE"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
一切正常,除了一个
Marker
我遵循这个网站的相关问题和网页上的指示,但都是徒劳的。地图上没有显示标记
请告诉我怎么了。
编辑:这里有
activity_maps
代码,其中包含MapFragment
。<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/b1_text"
/>
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/b2_text"
android:layout_toRightOf="@+id/b1"/>
<Button android:id="@+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/b3_text"
android:layout_toRightOf="@+id/b2"/>
<RelativeLayout
android:id="@+id/mainP"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_below="@+id/b2">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>
<EditText
android:id="@+id/info"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:hint="@string/info_1"
android:layout_below="@+id/mainP"
/>
<EditText
android:layout_width="160dp"
android:layout_height="wrap_content"
android:hint="@string/info_2"
android:layout_below="@+id/mainP"
android:layout_toRightOf="@+id/info"/>
</RelativeLayout>
最佳答案
首先,您应该在googleMap Object
中初始化onCreate(....)
SupportMapFragment googleMap=(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);